我在尝试使用Swing创建一个简单的UI时遇到了一些问题。这是我想要获得的窗口:
我想在两侧有两个可滚动的JTextAreas,并且在窗口中间还有两个按钮。
这是我的代码:
public class MainWindow extends JFrame implements ActionListener {
private JTextArea inputArea;
private JTextArea outputArea;
private JButton encriptButton, decriptButton;
public MainWindow() {
super();
}
public void initUI() {
this.setSize(900, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputArea = new JTextArea();
JScrollPane js = new JScrollPane(inputArea);
js.setPreferredSize(new Dimension(350,400));
outputArea = new JTextArea();
JScrollPane js2 = new JScrollPane(outputArea);
js2.setPreferredSize(new Dimension(350, 400));
JPanel panel = new JPanel();
encriptButton = new JButton("Encript");
decriptButton = new JButton("Decript");
panel.add(encriptButton);
panel.add(decriptButton);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(js, BorderLayout.WEST);
this.getContentPane().add(js2, BorderLayout.EAST);
this.getContentPane().add(panel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
现在,如果我做这样的事情:
MainWindow window = new MainWindow();
window.initUI();
window.setVisible(true);
我得到以下结果:
我不知道如何将两个按钮放在窗口的中央。我也不知道如何为JTextArea设置固定大小。而已。 提前谢谢!
答案 0 :(得分:4)
考虑:
所有这一切的关键是尝试可视化布局管理器应该做什么,然后嵌套JPanels,每个JPanels都有自己的布局管理器,以实现目标。
例如,这个GUI
可以通过以下代码生成:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class LayoutEg extends JPanel {
private static final long serialVersionUID = 1L;
private static final int ROWS = 30;
private static final int COLS = 30;
private static final int GBC_GAP = 10;
private JTextArea textArea1 = new JTextArea(ROWS, COLS);
private JTextArea textArea2 = new JTextArea(ROWS, COLS);
private JButton encryptButton = new JButton("Encrypt");
private JButton decryptButton = new JButton("Decrypt");
public LayoutEg() {
JPanel centerPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.insets = new Insets(GBC_GAP, GBC_GAP, GBC_GAP, GBC_GAP);
// setting the weightx and weighty of 0 is what forces the buttons
// in the center to bunch together
gbc.weightx = 0;
gbc.weighty = 0;
gbc.gridx = 0;
gbc.gridy = 0;
centerPanel.add(encryptButton, gbc);
gbc.gridy = 1;
centerPanel.add(decryptButton, gbc);
setBorder(BorderFactory.createEmptyBorder(GBC_GAP, GBC_GAP, GBC_GAP, GBC_GAP));
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
add(new JScrollPane(textArea1));
add(centerPanel);
add(new JScrollPane(textArea2));
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LayoutEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
答案 1 :(得分:2)
您可能需要考虑使用GridBagLayout来实现此设计,因为它可以更好地控制您希望按钮显示的位置。你可以在GridBagConstraints中实际进行单元格填充,或者你可以考虑使用Border(s)。