Java JTabbedPane向选项卡添加不同的类构造函数
我正在尝试创建一个包含选项卡和菜单栏的框架,每个选项卡将包含不同的Java JTabbedPane向选项卡添加不同的类构造函数,java,swing,constructor,jtabbedpane,Java,Swing,Constructor,Jtabbedpane,我正在尝试创建一个包含选项卡和菜单栏的框架,每个选项卡将包含不同的JTable,我想知道是否可以将不同类的构造函数添加到选项卡中? 我的意思是: 然后是第二(第一)类: 编辑 好的,我找到了一种方法,但是现在我有一个不同的问题,我如何使用主类中的JMenuBar?我扩展了JPanel,而不是类,我可以在其他类中使用它们吗 使用JMenuBar是什么意思?我建议研究OOP和封装。YouTube上有个家伙,我认为他在这方面做得很好,涵盖了很多设计模式,我相信这个频道的名字是DerekBanas。如果
JTable
,我想知道是否可以将不同类的构造函数添加到选项卡中?
我的意思是:
然后是第二(第一)类:
编辑
好的,我找到了一种方法,但是现在我有一个不同的问题,我如何使用主类中的
JMenuBar
?我扩展了JPanel,而不是类,我可以在其他类中使用它们吗 使用JMenuBar是什么意思?我建议研究OOP和封装。YouTube上有个家伙,我认为他在这方面做得很好,涵盖了很多设计模式,我相信这个频道的名字是DerekBanas。如果你用谷歌搜索,他会出现的
对您的问题的一个快速回答是,您需要将某些内容传递到JPanel类的构造函数中,或者将main方法放在JPanel类中,下面是一个快速的示例,其中一种方法是
JMenuItem是-添加新记录,在第一个选项卡中,当我选择这个JMenuItem时,他会为我打开一个框架,其中是JLabel和JTextField。然后是第二个选项卡,我单击相同的JMenuItem,它是一个新的JFrame,它有自己的JLabel和JTextField
public class TestFrame extends JFrame {
private ActionManager actionManager
private JMenuBar mb;
private JMenu file;
private JMenuItem openDialog1;
private JMenuItem openDialog2;
public TestFrame() {
this.actionManager = new ActionManager();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPanel(panel);
setJMenuBar(createMenuBar());
pack();
}
private JMenuBar createMenuBar() {
mb = new JMenuBar();
file = new JMenu("File");
openDialog2 = new JMenuItem("Open Dialog 2");
openDialog1 = new JMenuItem("Open Dialog 1");
openDialog2.addActionListener(actionManager.openDialog2Action);
openDialog1.addActionListener(actionManager.openDialog1Action);
//here i would add conditional code that added the correct
//menus to the menubar and menuitems to the correct menus and call
//this method from a `ChangeListener` that listens for tab changes so
//it recreates a new menu with the correct components for the selected
//tab but i added them to the same menu to demonstrate using specific actions
//for each menu item, it doesn't matter which menu they're attached to their
//action wont be called unless that JMenuItem is clicked.
//this method can be used for any type of button as well, commonly with
//toolbars, so you can reuse actions like copy, paste, new, open, save, etc
file.add(openDialog1);
file.add(openDialog1);
mb.add(file);
return mb;
}
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
还有小组课
public class ActionManager {
public ActionManager() {
}
Action openDialog1Action = new AbstractAction("Open Dialog 1") {
JOptionPane.showMessageDialog(null, "Dialog 1");
}
Action openDialog2Action = new AbstractAction("Open Dialog 2") {
JOptionPane.showMessageDialog(null, "Dialog 2");
}
}
这段代码所做的就是使用公共方法从类(封装)访问私有字段,并将TestFrame对象传递到TestPanel构造函数中,这样我们就可以从TestPanel类在TestFrame中使用getter方法。我还添加了ActionListener作为TestPanel类的一部分,因为这是在panel类中使用菜单栏组件的另一种方法。你能具体地告诉我你想要完成什么,或者可能发布你当前代码的一小部分吗?这是实现您要求的几种方法之一,但这可能不是首选方法,这取决于您首先希望从小组访问它们的原因,您的问题越具体,我就越有帮助。祝你好运。使用JMenuBar是什么意思?我建议研究OOP和封装。YouTube上有个家伙,我认为他在这方面做得很好,涵盖了很多设计模式,我相信这个频道的名字是DerekBanas。如果你用谷歌搜索,他会出现的 对您的问题的一个快速回答是,您需要将某些内容传递到JPanel类的构造函数中,或者将main方法放在JPanel类中,下面是一个快速的示例,其中一种方法是 JMenuItem是-添加新记录,在第一个选项卡中,当我选择这个JMenuItem时,他会为我打开一个框架,其中是JLabel和JTextField。然后是第二个选项卡,我单击相同的JMenuItem,它是一个新的JFrame,它有自己的JLabel和JTextField
public class TestFrame extends JFrame {
private ActionManager actionManager
private JMenuBar mb;
private JMenu file;
private JMenuItem openDialog1;
private JMenuItem openDialog2;
public TestFrame() {
this.actionManager = new ActionManager();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPanel(panel);
setJMenuBar(createMenuBar());
pack();
}
private JMenuBar createMenuBar() {
mb = new JMenuBar();
file = new JMenu("File");
openDialog2 = new JMenuItem("Open Dialog 2");
openDialog1 = new JMenuItem("Open Dialog 1");
openDialog2.addActionListener(actionManager.openDialog2Action);
openDialog1.addActionListener(actionManager.openDialog1Action);
//here i would add conditional code that added the correct
//menus to the menubar and menuitems to the correct menus and call
//this method from a `ChangeListener` that listens for tab changes so
//it recreates a new menu with the correct components for the selected
//tab but i added them to the same menu to demonstrate using specific actions
//for each menu item, it doesn't matter which menu they're attached to their
//action wont be called unless that JMenuItem is clicked.
//this method can be used for any type of button as well, commonly with
//toolbars, so you can reuse actions like copy, paste, new, open, save, etc
file.add(openDialog1);
file.add(openDialog1);
mb.add(file);
return mb;
}
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
还有小组课
public class ActionManager {
public ActionManager() {
}
Action openDialog1Action = new AbstractAction("Open Dialog 1") {
JOptionPane.showMessageDialog(null, "Dialog 1");
}
Action openDialog2Action = new AbstractAction("Open Dialog 2") {
JOptionPane.showMessageDialog(null, "Dialog 2");
}
}
这段代码所做的就是使用公共方法从类(封装)访问私有字段,并将TestFrame对象传递到TestPanel构造函数中,这样我们就可以从TestPanel类在TestFrame中使用getter方法。我还添加了ActionListener作为TestPanel类的一部分,因为这是在panel类中使用菜单栏组件的另一种方法。你能具体地告诉我你想要完成什么,或者可能发布你当前代码的一小部分吗?这是实现您要求的几种方法之一,但这可能不是首选方法,这取决于您首先希望从小组访问它们的原因,您的问题越具体,我就越有帮助。祝你好运。我不知道你在问什么。您想将JMenuBar添加到JPanel中吗?如果是这样,您可以将JMenuBars添加到自定义组件中,我现在记不起来了,但我相信这是一个必须实现的接口,以允许它包含JMenu。如果您从JFrame开始查看API,并遵循超类,您将看到它列在接口中,可能在Window类或其超类的某个位置,它有一个明显的名称,如果我想到它,我会让您知道。如果这不是你所要求的,请更具体地说明你想做什么,我会尽力帮助你。祝你好运。它在Frame类中,接口是MenuContainer。我没有弄乱它,但我非常确定这将允许您向自定义容器添加JMenuBar。但我会考虑尝试用另一种方式来完成同样的事情,很多时候,当事情不经常被使用时,因为有一种更简单的方式去做它,尽管事实并非总是如此。只是想一想。例如:两个类,一个有一个JMenuBar,第二个扩展了JPanel,我可以在第二个类中使用第一个类JMenuBar吗?现在我不能,因为二等舱不能扩展一等舱。我回答这个问题是为了给你们一个简短的示例程序。在该程序中,菜单在frame类中创建,对它及其组件的引用存储在panel类中。你应该能够在panel类中使用菜单栏的任何部分,就像在frame类中一样。我不确定你在问什么。您想将JMenuBar添加到JPanel中吗?如果是这样,您可以将JMenuBars添加到自定义组件中,我现在记不起来了,但我相信这是一个必须实现的接口,以允许它包含JMenu。如果您从JFrame开始查看API,并遵循超类,您将看到它列在接口中,可能在Window类或其超类的某个位置,它有一个明显的名称,如果我想到它,我会让您知道。如果这不是你所要求的,请更具体地说明你想做什么,我会尽力帮助你。祝你好运。它在Frame类中,接口是MenuContainer。我没有弄乱它,但我非常确定这将允许您向自定义容器添加JMenuBar。但我会考虑用另一种方式来完成同样的事情,很多时候,当事情没有被经常使用时,因为有一个更简单的W。