简体   繁体   English

Jbutton定位在选项卡式窗格内

[英]Jbutton positioning inside a tabbed pane

I am currently working on making a digital school planner and want to use multiple JButtons inside a JTabbedPane. 我目前正在制作数字学校计划器,并想在JTabbedPane中使用多个JButton。 But I am currently having a problem where even one button takes up the whole pane and would like a solution to this. 但是我目前遇到一个问题,即使是一个按钮也占据了整个窗格,并且想要解决这个问题。

I have created a test program that mirrors how i am coding my main program: 我创建了一个测试程序,该程序反映了我如何编写主程序:

 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();

    }
}

i have tried using BorderLayout.POSITION and it chucked up error after error any alternate solution would be great :) 我已经尝试使用BorderLayout.POSITION并且它在出错后取消了错误,任何其他解决方案都很棒:)

Your tabbedPane can only hold one "object". 您的tabbedPane只能容纳一个“对象”。 If you add a button, your pane is full. 如果添加按钮,则窗格已满。 However you can add a container with more room to the pane. 但是,您可以将具有更多空间的容器添加到窗格中。 For example a JPanel . 例如一个JPanel The JPanel can now hold as many "objects" as you want and can use it's own LayoutManager. 现在,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); 

Also you crete and initialize your whole GUI inside the constructor. 您还可以在构造函数内部创建并初始化整个GUI。 It might be cleaner to define a second methode like initializeGUI() to seperate the creation of the object and the initilazation of your GUI. 定义第二种方法(例如initializeGUI()以分隔对象的创建和GUI的initializeGUI()可能更干净。 Also you should seperate the GUI-thread and the Main-thread, by using invokeLater 另外,您还应该使用invokeLater分隔GUI线程和主线程。

public static void main(String[] args) {
   SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    Gui g = new Gui();
    g.initalizeGUI();
    Gui.setVisible(true);
  }
  });
}

There is no need to extend JFrame and to create a seperate JFrame inside your JFrame. 无需扩展JFrame并在JFrame中创建单独的JFrame。 Remove the extends JFrame or the creation of your JFrame inside the class. 在类内删除extends JFrame或创建JFrame。 You can access the JFrame mehtodes from inside your extended class. 您可以从扩展类内部访问JFrame方法。

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);
}
}

Add another container like JPanel inside your JTabbedPane like this: 在您的JTabbedPane添加另一个容器,例如JPanel如下所示:

    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);

Add all your components to the JPanel that you want to show on the tabs. 将所有组件添加到要在选项卡上显示的JPanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM