[英]JScrollPane isn't visible
滚动窗格在此处不可见。 我知道问题是setlayout(null)
。 我可以写些什么呢?
labelsend.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e3){
framelogin.setVisible(false);
JFrame framesend = new JFrame();
framesend.setSize(500,500);
JPanel panelsend = new JPanel();
panelsend.setLayout(null);
JLabel labelsendname = new JLabel("Name: ");
labelsendname.setBounds(20,20,50,10);
panelsend.add(labelsendname);
JTextField textsendname = new JTextField();
textsendname.setBounds(60, 15, 400, 18);
panelsend.add(textsendname);
JLabel labelmessagesend = new JLabel("Message: ");
labelmessagesend.setBounds(1,50,80,14);
panelsend.add(labelmessagesend);
JTextArea textmessagesend = new JTextArea(5,10);
textmessagesend.setBounds(60,50,400,300);
JScrollPane scrollsend = new JScrollPane(textmessagesend);
scrollsend.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textmessagesend.setWrapStyleWord(true);
textmessagesend.setLineWrap(true);
panelsend.add(scrollsend);
framesend.add(panelsend);
framesend.setVisible(true);
framesend.add(panelsend);
framesend.setVisible(true);
}
});
我可以写些什么呢?
布局。 对于这一点,我将使用带有约束的GridBagLayout
,该约束将为文本区域提供剩余的可用空间,但要遵循文本字段的初始大小(即不要扩展它)。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class SendFrame2 {
private JComponent ui = null;
JLabel labelsendname = new JLabel("Name: ", JLabel.TRAILING);
JTextField textsendname = new JTextField(20); //suggest a size in columns
JLabel labelmessagesend = new JLabel("Message: ", JLabel.TRAILING);
JTextArea textmessagesend = new JTextArea(15, 45);
SendFrame2() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
GridBagConstraints gbc = new GridBagConstraints(
0, 0, 1, 1, 0, 0,
GridBagConstraints.BASELINE_TRAILING,
GridBagConstraints.BOTH,
new Insets(5,5,5,5),
4, 2);
ui = new JPanel(new GridBagLayout());
ui.setBorder(new EmptyBorder(10, 10, 10, 10));
ui.add(labelsendname, gbc);
gbc.gridy = 1;
ui.add(labelmessagesend, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
ui.add(textsendname, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
JScrollPane jsp = new JScrollPane(
textmessagesend,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
ui.add(jsp, gbc);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
SendFrame2 o = new SendFrame2();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.