Jbutton定位在选项卡式窗格中

时间:2016-09-23 14:29:55

标签: java jbutton jtabbedpane

我目前正致力于制作数字学校规划师,并希望在JTabbedPane中使用多个JButton。但我现在遇到的问题是,即使一个按钮占用整个窗格,也想要解决这个问题。

我创建了一个测试程序,它反映了我对主程序的编码方式:

 import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class tabTest extends JFrame {
    private static final long serialVersionUID = 5101892517858668104L;
    private JFrame frame;
    private int WIDTH = 450;
    private int HEIGHT = 600;

    private JTabbedPane tab;

    public tabTest() {

        frame = new JFrame();
        // frame.setUndecorated(true);
        /****************************************************
         * Set up frame
         *****************************************************/
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocation(50, 50);
        frame.getContentPane().setBackground(Color.GRAY);
        frame.getContentPane().setLayout(null);

        /******************************************************
         * Set up Tabbed pane and buttons
         ******************************************************/
        tab = new JTabbedPane();
        tab.setBounds(20, 50, 400, 500);
        tab.setBackground(Color.white);
        tab.setFocusable(false);

        JButton tabButton1 = new JButton("test");

        tab.addTab("Week 1", tabButton1);
        tab.addTab("Week 2", null);
        tab.addTab("Week 3", null);
        tab.addTab("Week 4", null);
        frame.getContentPane().add(tab);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        new tabTest();

    }
}

我已经尝试过使用BorderLayout.POSITION并且在错误发生错误后,任何替代解决方案都会很棒:)

2 个答案:

答案 0 :(得分:0)

您的tabbedPane只能容纳一个“对象”。如果添加按钮,则窗格已满。但是,您可以向窗格添加具有更多空间的容器。例如 JPanel 。 JPanel现在可以根据需要保存任意数量的“对象”,并且可以使用它自己的LayoutManager。

JPanel moreButtons = new JPanel();
moreButtons.add(new JButton("test1"));
moreButtons.add(new JButton("test2"));
moreButtons.add(new JButton("test3"));
moreButtons.add(new JButton("test4"));

tab.addTab("Week 1", moreButtons); 

您也可以在构造函数中创建并初始化整个GUI。定义像initializeGUI()这样的第二个方法可以更清楚地分离对象的创建和GUI的初始化。您还应该使用invokeLater

分隔GUI线程和主线程
public static void main(String[] args) {
   SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    Gui g = new Gui();
    g.initalizeGUI();
    Gui.setVisible(true);
  }
  });
}

无需扩展JFrame并在JFrame中创建单独的JFrame。删除班级中的extends JFrame或JFrame的创建。您可以从扩展类中访问JFrame mehtodes。

public class MyFrame extends JFrame {

public MyFrame () {
    /**
     * Set up frame 
     */
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(WIDTH, HEIGHT);
    setLocation(50, 50);
    getContentPane().setBackground(Color.GRAY);
    getContentPane().setLayout(null);
}
}

答案 1 :(得分:0)

JPanel中添加JTabbedPane之类的其他容器,如下所示:

    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());   // you can change layout according to your need.
    p.add(tabButton1);
    tab.addTab("Week 1", panel);
    tab.addTab("Week 2", null);
    tab.addTab("Week 3", null);
    tab.addTab("Week 4", null);

将所有组件添加到要在标签页上显示的JPanel

相关问题