gpt4 book ai didi

java - JPanels 未添加到 JTabbedPane

转载 作者:行者123 更新时间:2023-11-30 06:22:34 26 4
gpt4 key购买 nike

为了我的家庭作业加分,我创建了一个 JTabbedPane 并添加了两个 Jpanel。我觉得我已经很接近了,但它仍然无法编译。当我运行它时,两个 JPanel 都会打开,但 JTabbedPane 不会。我收到很多未知来源错误。我怀疑此时我的问题出在 JPanel 本身,因为它们开始是 JFrames,而我已经尝试(我认为未成功)将 JFrames 转换为 JPanel。

JTabbedPaneAssignment 应该创建 JTabbedPane 并在两个 Pane 中填充一个选项卡上的应用程序 DayGui 和另一个选项卡上的 OfficeAreaCalculator。我在这里只包含 JTabbedPaneAssignment 和 DayGui 类。如果它的代码太多,我深表歉意,我已经删掉了很多我认为无关的东西,但可能还是太多了。

这里是调用类 JTabbedPaneAssignment

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTabbedPaneAssignment extends JPanel
{
public JTabbedPaneAssignment()
{
//super(new GridLayout(1, 1));

JTabbedPane tabbedPane = new JTabbedPane();
DayGui pnlDay = new DayGui();
OfficeAreaCalculator pnlOffice = new OfficeAreaCalculator ();
this.add(tabbedPane);

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
tabbedPane.add(panel1,"First panel");
tabbedPane.add(panel2,"Second panel");


//JComponent panel1 = makeTextPanel("Pane #1");
panel1.setPreferredSize(new Dimension(300, 150));
tabbedPane.addTab("DayGui", panel1);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

//JComponent panel2 = makeTextPanel("Pane #2");
panel2.setPreferredSize(new Dimension(410, 50));
tabbedPane.addTab("OfficeAreaCalculator", panel2);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
add(tabbedPane);
}

protected JComponent makeTextPanel(String text)
{
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}

// Create JTabbedPane
private static void createAndShowGUI()
{
// Create and set up the window.
JFrame frame = new JFrame("JTabbedPane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTabbedPaneAssignment(), BorderLayout.CENTER);
JTabbedPane DayGui = new JTabbedPane();
JTabbedPane OfficeAreaCalculator = new JTabbedPane();
frame.pack();
frame.setVisible(true);
}


public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

这是 DayGui 类。它最初是一个 JFrame,但我试图将它转换为 JPanel。我认为我的问题出在这部分代码中,但我不知道

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

//public class DayGui extends JPanel
public class DayGui extends JPanel
{
private JPanel dayPanel;
private JButton cmdGood;
private JButton cmdBad;

public DayGui()
{
//dayPanel = new JPanel("Messages");

cmdGood = new JButton("Good");
cmdBad = new JButton("Bad");

Container c = dayPanel.getRootPane();
c.setLayout(new FlowLayout());

c.add(cmdGood);
c.add(cmdBad);

dayPanel.setSize(300, 150);

ButtonsHandler bhandler = new ButtonsHandler();
cmdGood.addActionListener(bhandler);
cmdBad.addActionListener(bhandler);

dayPanel.setVisible(true);
dayPanel.getRootPane().setBackground(Color.CYAN);

}

class ButtonsHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == cmdGood)
JOptionPane.showMessageDialog(null, "Today is a good day!",
"Event Handler Message",
JOptionPane.INFORMATION_MESSAGE);
if (e.getSource() == cmdBad)
JOptionPane.showMessageDialog(null, "Today is a bad day!",
"Event Handler Message",
JOptionPane.INFORMATION_MESSAGE);
}
}

}

最佳答案

你的问题一直很模棱两可,而且你发布的代码比这里的任何人有时间浏览的都多。

我制作了一个非常小但有效的 JTabbedPane 示例,让您了解最小的工作量以使其正常工作。

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class JTabbedPaneExample extends JFrame{

public JTabbedPaneExample(String title){
super(title);
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

JTabbedPane tabbedPane = new JTabbedPane();

JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
tabbedPane.add(panelOne,"First panel");
tabbedPane.add(panelTwo,"Second panel");

add(tabbedPane);
}


public static void main(String[] args){

new JTabbedPaneExample("JTP Example").setVisible(true);

}
}

然后运行,这段代码看起来像:

enter image description here

如果您在代码中重复相同的过程,但仍然有错误,则问题不在于您的 JTabbedPane,而在于其他方面。

关于java - JPanels 未添加到 JTabbedPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19119690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com