欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GUI interface的设计

程序员文章站 2022-05-28 11:36:49
...
//menu
package datapro;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Menu  {
    public static void main(String[] arg) throws Exception{
        final JFrame  jf=new JFrame("测试窗口");
        jf.setSize(300, 300);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setVisible(true);

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("menu");
        menuBar.add(fileMenu);


        JMenuItem fiMenuItem = new JMenuItem("align");
        JMenuItem seMenuItem = new JMenuItem("calculator");
        JMenuItem exitMenuItem = new JMenuItem("exit");
        fileMenu.add(fiMenuItem);
        fileMenu.add(seMenuItem);
        fileMenu.addSeparator();       // 添加一条分割线
        fileMenu.add(exitMenuItem);

        LabelFrame panel1 = new LabelFrame();
        PanelFrame panel2 = new PanelFrame();
        jf.add(panel2);
        panel2.setVisible(false);
        //panel2.setVisible(false);
        jf.add(panel1);
        panel1.setVisible(false);

        fiMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jf.remove(panel1);
                jf.add(panel2);
                panel2.setVisible(true);    

                          }
        });

        seMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jf.remove(panel2);
                jf.add(panel1);
                panel1.setVisible(true);

            }
        });


        jf.setJMenuBar(menuBar);
        jf.setVisible(true);
    }
}

first
//
package datapro;  
import java.awt.*;  
    import javax.swing.*;  
    public class LabelFrame extends JPanel  
    {  
        private final JButton []buttons;  
        private static final String []names = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};  
        JTextField displayField=new JTextField(20);   
        JPanel p = new JPanel (new GridLayout (4,4,3,3));  
        public LabelFrame()  
        {  
           // super("Calculator");  
            setLayout (new BorderLayout());  
            buttons = new JButton[names.length];  
            for (int i=0;i<names.length;i++) {  
                buttons[i] = new JButton (names[i]);  
                p.add (buttons[i]);  
            }  
            this.add (displayField,BorderLayout.NORTH);  
            this.add (p,BorderLayout.CENTER);  

        }  

    }  
//
package datapro;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.event.*;
public class PanelFrame extends JPanel {
    private final JLabel label1;
    private final JLabel label2;
    private JTextField Field1;
    private JTextField Field2;
    private JButton Button1 = new JButton("   Ok    ");
    private JButton Button2 = new JButton("Cancle");
    private JButton Button3 = new JButton("  Help  ");
    public final JCheckBox checkbox1;
    public final JCheckBox checkbox2;
    protected BorderLayout layout;
    //protected Container container;

    public PanelFrame()
    {
        //super("Align");
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));    
        JPanel panel2 = new JPanel();
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        JPanel panel3 = new JPanel();
        panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
        JPanel panel6 = new JPanel();
        panel6.setLayout(new BoxLayout(panel6, BoxLayout.Y_AXIS));
        JPanel panel4 = new JPanel();
        panel4.setLayout(new FlowLayout());
        JPanel panel5 = new JPanel();
        //panel5.setLayout(new BoxLayout(panel5, BoxLayout.X_AXIS));
        panel5.setLayout(new FlowLayout());
        layout=new BorderLayout();
        //container=getContentPane();
        setLayout(layout);
        Box box1 = Box.createVerticalBox();
        //Dimension preferredSize = new Dimension(70,40);
        panel.add(box1);
        box1.add(Box.createRigidArea(new Dimension(15,30)));
        box1.add(Button1);
        box1.add(box1.createVerticalStrut(20));
        box1.add(Button2);
        //box1.createVerticalStrut(100);
        box1.add(box1.createVerticalStrut(20));
        box1.add(Button3);
        this.add(panel,BorderLayout.EAST);
        Box box2 = Box.createVerticalBox();
        box2.add(Box.createRigidArea(new Dimension(20,50)));
        panel2.add(box2);
        checkbox1 = new JCheckBox("snap to Grid");
        box2.add(checkbox1);
        box2.add(box2.createVerticalStrut(10));
        checkbox2 = new JCheckBox("show Grid");
        box2.add(checkbox2);
        this.add(panel2, BorderLayout.WEST);
        Box box3 = Box.createVerticalBox();

        box3.add(Box.createRigidArea(new Dimension(70,30)));
        panel3.add(box3);
        Box box4 = Box.createVerticalBox();
        //box4.add(box4.createHorizontalStrut(20));
        //panel6.add(box4);
        //this.add(panel6, BorderLayout.EAST);
//      Box box5 = Box.createVerticalBox();
        label1 = new JLabel("X:");
        Field1 = new JTextField(5);
        panel4.add(label1);
        panel4.add(Field1);
        box3.add(panel4);
//      box4.add(label1);
//      Field1.setPreferredSize(new Dimension(10,40));
//      box5.add(Field1);
//      panel4.add(box4);
//      panel5.add(box5);
//      box3.add(panel4);
        label2 = new JLabel("Y:");
        Field2 = new JTextField(5);
        panel5.add(label2);
        panel5.add(Field2);
        box3.add(panel5);
        //box3.add(panel5);
        //label2 = new JLabel("Y:");
        //Field2 = new JTextField(5);
        //Box box5 = Box.createVerticalBox();
        //box5.add(label2);
        //box5.add(Field2);
        //panel5.add(box5);

        this.add(panel3, BorderLayout.CENTER);
    }

}

GUI interface的设计

GUI interface的设计

相关标签: gui