我在使用此GUI时遇到了很多麻烦。我将要一个applet,左边是panel1,右边是panel2。这是我的两个问题:1)JTextArea需要占用applet的整个右侧,但我无法做到这一点。 2)每当我使JTextArea更长时,JTextField也会变长,即使它在一个完全不同的面板中。这是怎么回事?小程序和一切都在另一个类中创建 - 它编译并运行良好。我只是不能让这部分工作。
private JButton button1;
private JLabel label1;
private JTextField field1;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JTextArea area1;
int i = 0;
int j = 0;
private JScrollPane pane;
public Class()
{
this.petList = petList;
this.sPanel = sPanel;
panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
button1 = new JButton("Test");
panel1.add(button1,BorderLayout.SOUTH);
field1 = new JTextField();
panel1.add(field1, BorderLayout.EAST);
label1 = new JLabel("Test");
panel1.add(label1, BorderLayout.WEST);
panel2 = new JPanel();
area1 = new JTextArea(10, 20);
panel2.add(area1);
panel3 = new JPanel();
panel3.setLayout(new BorderLayout(5,5));
panel3.add(panel1, BorderLayout.WEST);
panel3.add(panel2, BorderLayout.EAST);
this.add(panel3);
答案 0 :(得分:5)
您的想法很好,复合布局使得布局多个组件变得更加容易,但您选择的布局管理器有点不对。
BorderLayout
的本质是使用所有可用空间来布局它的组件。
尝试使用类似GridBagLayout
的内容
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BadLayout12 {
public static void main(String[] args) {
new BadLayout12();
}
public BadLayout12() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JButton button1;
private JLabel label1;
private JTextField field1;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JTextArea area1;
int i = 0;
int j = 0;
private JScrollPane pane;
public TestPane() {
// this.petList = petList;
// this.sPanel = sPanel;
button1 = new JButton("Test");
field1 = new JTextField(10);
label1 = new JLabel("Test");
panel1 = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
panel1.add(label1, gbc);
gbc.gridx++;
panel1.add(field1, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 2;
panel1.add(button1, gbc);
panel2 = new JPanel();
area1 = new JTextArea(10, 20);
panel2.add(new JScrollPane(area1));
panel3 = new JPanel();
panel3.setLayout(new BorderLayout(5, 5));
panel3.add(panel1, BorderLayout.WEST);
panel3.add(panel2, BorderLayout.EAST);
this.add(panel3);
}
}
}
看看;
了解更多详情