如何从tabbedpane设计面板并使面板居中

时间:2016-03-17 20:16:12

标签: java jframe jpanel jtabbedpane

i want to make a bigger resolution/fullscreen but i dont know what layout should i use, can u guys help me to choose a better layout 这是第一张图片的第一个代码

    tp = new JTabbedPane();
    tp.setBounds(0,0,250,400);
    panel = new JPanel();
    panel.setPreferredSize(new Dimension(250, 480));
    tp.addTab("Mahasiswa",panel);
    panel.setLayout(new FlowLayout());
    label_insert = new JLabel("NIM :               ");
    label_insert2 = new JLabel("ID Jurusan :   ");
    tf_insert1 = new JTextField(15);
    tf_insert2 = new JTextField(15);
    Insert = new JButton(" Insert ");
    //panel add

this one i want to make it on middle of frame and all of them already on panel, how can i move it to the middle ? 这是第二张图片的代码

    public void mainform(){

    main = new JPanel();
    main.setLayout(new FlowLayout(FlowLayout.CENTER));
    label_main = new JLabel("ID");
    label_main2 = new JLabel("Password");
    tf_main = new JTextField(15);
    tf_main2 = new JPasswordField(15);
    login = new JButton("LOGIN");
    login.addActionListener(this);
    main.add(label_main);
    main.add(tf_main);
    main.add(label_main2);

2 个答案:

答案 0 :(得分:0)

Following this guide here我建议如下:

yourFrame = new JFrame();
yourFrame.setLayout(new BorderLayout());

tp = new JTabbedPane();
yourFrame.add(tp, BorderLayout.CENTER);

然后为tabbedpane中的标签提供GridLayout,GridBagLayout或GroupLayout。

答案 1 :(得分:0)

我会考虑使用GridBagLayout,这将为您提供更灵活的组件布局方式

Layout

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);

            add(new JLabel("ID"), gbc);
            gbc.gridx++;
            add(new JTextField(20), gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            add(new JLabel("Password"), gbc);
            gbc.gridx++;
            add(new JTextField(20), gbc);
            gbc.gridx = 0;
            gbc.gridy++;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.CENTER;
            add(new JButton("Login"), gbc);
        }

    }

}

有关详细信息,请参阅Laying Out Components Within a ContainerHow to Use GridBagLayout