Reputation: 3
The scroll pane isn't visible here. I know that the problem is setlayout(null)
. What can I write instead of this?
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);
}
});
Upvotes: 0
Views: 30
Reputation: 168825
What can I write instead of this?
Layouts. For this one, I'd use a GridBagLayout
with constraints that will give the text area the remainder of the available space, but honor the initial size of the text field (i.e. don't expand it).
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);
}
}
Upvotes: 1