如何使用Java Swing制作GUI

时间:2016-12-21 21:02:38

标签: java swing user-interface

所以我最近开始学习面向对象的编程,在Java中做了最基本的东西,现在我想从控制台编写转换到实际的GUI。

但是我有理解它是如何工作的问题,我在oracle文档中阅读了所有内容,但是他们的例子并不那么清晰易懂,也用Google搜索,但无法找到任何好的解释。这是我的最后一招。

以下是我的程序GUI应该如何显示的想法: ![像这样] [1]

我画得不太好但是所有的复选框都应该对齐。

如果有人可以向我提供很好的代码解释,如果不只是一些好的解释会对我有很大的帮助,我的问题是,当我对齐其他内容时,其他内容会搞砸。

我目前的代码:

public class Zadatak2 extends JFrame{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here


        new Zadatak2();

    }


    public Zadatak2(){

        JPanel panel1 = new JPanel();
        setSize(500, 250);
        setLocationRelativeTo(null);
        add(panel1);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);




        BorderLayout border1 = new BorderLayout();
        panel1.setLayout(border1);


        JPanel panel2 = new JPanel();
        GridLayout grid1 = new GridLayout(3,2);

        panel2.setLayout(grid1);
        panel2.add(new JCheckBox("Bold"));
        panel2.add(new JCheckBox("Italic"));
        panel2.add(new JCheckBox("Underline"));


        panel1.add(panel2,BorderLayout.WEST);



        JPanel panel3 = new JPanel();
        GridLayout grid2 = new GridLayout(4,1);
        panel3.setLayout(grid2);
        panel3.add(new JCheckBox("Strikerthrough"));
        panel3.add(new JCheckBox("Teletype"));
        panel3.add(new JCheckBox("Emphasis"));
        panel3.add(new JCheckBox("Strong"));
        panel1.add(panel3,BorderLayout.CENTER);

        JPanel panel4 = new JPanel();
        BorderLayout border2 = new BorderLayout();
        panel4.setLayout(border2);

        panel4.add(new JButton("Apply"), BorderLayout.EAST);
        panel4.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 70));
        panel1.add(panel4,BorderLayout.SOUTH);

        JPanel panel5 = new JPanel();
        GridLayout grid3 = new GridLayout(2,1);
        panel5.setLayout(grid3);
        panel5.add(new JLabel("Sample text"));
        JTextArea ta = new JTextArea("Sample text");
        panel5.setBorder(BorderFactory.createEmptyBorder(5, 0, 10, 30));
        ta.setSize(200, 200);
        panel5.add(ta);
        panel1.add(panel5,BorderLayout.EAST);


        setVisible(true);
    }

}

感谢大家的帮助,最后让gui成为了正确的,现在我明白了更好。

2 个答案:

答案 0 :(得分:3)

要将复选框放在GridLayout中,您不应为每列定义面板。只需像这样定义和放置它们:

JPanel panel2 = new JPanel();
GridLayout grid1 = new GridLayout(4, 2);

panel2.setLayout(grid1);
panel2.add(new JCheckBox("Bold"));
panel2.add(new JCheckBox("Strikerthrough"));
panel2.add(new JCheckBox("Italic"));
panel2.add(new JCheckBox("Teletype"));
panel2.add(new JCheckBox("Underline"));
panel2.add(new JCheckBox("Emphasis"));
panel2.add(new JLabel(""));  // fill the empty cell with an empty label
panel2.add(new JCheckBox("Strong"));

但是,由于您的TextArea应与复选框对齐并跨越多行,我建议您使用GridBagLayout。它需要编写更多代码,但比GridLayout更灵活。

一个例子:

 public Zadatak2() {

    JPanel panel1 = new JPanel();
    setSize(500, 250);
    setLocationRelativeTo(null);
    add(panel1);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    BorderLayout border1 = new BorderLayout();
    panel1.setLayout(border1);

    JPanel panel2 = new JPanel();
    GridBagLayout grid1 = new GridBagLayout();

    panel2.setLayout(grid1);
    panel2.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    Insets insets = new Insets(2, 2, 2, 2);

    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.insets = insets;
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JCheckBox("Bold"), c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 0;
    c.insets = insets;
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JCheckBox("Strikerthrough"), c);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 1;
    c.insets = insets;
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JCheckBox("Italic"), c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 1;
    c.insets = insets;
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JCheckBox("Teletype"), c);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 2;
    c.insets = insets;
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JCheckBox("Underline"), c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 2;
    c.insets = insets;
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JCheckBox("Emphasis"), c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 3;
    c.insets = insets;
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JCheckBox("Strong"), c);

    insets = new Insets(2, 40, 2, 2);
    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 0;
    c.insets = insets;
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JLabel("Sample text"), c);

    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 1;
    c.insets = insets;
    c.weightx = .5;
    c.fill = GridBagConstraints.BOTH;
    c.gridheight = 3;
    JTextArea ta = new JTextArea("Sample text", 4, 4);
    JScrollPane sp = new JScrollPane(ta);
    panel2.add(sp, c);

    c = new GridBagConstraints();
    c.gridx = 2;
    c.gridy = 5;
    c.insets = new Insets(40, 40, 2,2);
    c.anchor = GridBagConstraints.WEST;
    panel2.add(new JButton("Apply"), c);


    panel1.add(panel2, BorderLayout.CENTER);

    pack();

    setVisible(true);
}

结果:

Screenshot of UI using GridBagLayout

请参阅GridBagLayout TutorialJavaDoc,了解此布局的所有选项。

答案 1 :(得分:2)

请记住使用自己的布局嵌套JPanel。例如,您的图像化GUI可以采用多种方式之一。整个JPanel可以使用面向该行的BoxLayout,并且在左侧使用JPLnel使用JPanel,右边是使用JPanel的BorderLayout。例如:

enter image description here

其他问题:

  • 应将JTextArea放入JScrollPane,然后将滚动窗格添加到GUI中。
  • 从不设置文本组件的大小或首选大小,尤其是JTextAreas,因为这会阻止它们正常工作,尤其是在滚动窗格内。
  • 而是设置JTextArea的列和行属性。
  • 一般不要设置尺寸。在添加所有内容并将其设置为可见之前,让GUI组件和布局通过在JFrame上调用pack()来设置自己的大小。

例如,稍加修改:

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.*;

@SuppressWarnings("serial")
public class SimpleGui extends JPanel {
    private static final String[] CHK_BOX_TEXTS = {"Monday", "Tuesday", "Wednesday", 
            "Thursday", "Friday", "Saturday", "Sunday"};
    private String title = "My Title";
    private JTextArea textArea = new JTextArea(10, 30);
    private JButton applyButton = new JButton("Apply");

    public SimpleGui() {
        // gridlayout with 2 columns, variable rows
        JPanel checkBoxPanel = new JPanel(new GridLayout(0, 2, 4, 4));
        for (String chkBoxText : CHK_BOX_TEXTS) {
            checkBoxPanel.add(new JCheckBox(chkBoxText));
        }
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
        leftPanel.add(Box.createVerticalStrut(20));
        leftPanel.add(checkBoxPanel);
        leftPanel.add(Box.createGlue());        

        JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
        titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 20));
        JScrollPane scrollPane = new JScrollPane(textArea);
        JPanel btnPanel = new JPanel();
        btnPanel.add(applyButton);

        JPanel rightPanel = new JPanel(new BorderLayout(5, 5));
        rightPanel.add(titleLabel, BorderLayout.PAGE_START);
        rightPanel.add(scrollPane, BorderLayout.CENTER);
        rightPanel.add(btnPanel, BorderLayout.PAGE_END);        

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        add(leftPanel);
        add(rightPanel);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("SimpleGui");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new SimpleGui());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

显示为:

enter image description here