我正在创建一个包含四个不同标签的应用程序。我没有将所有四个选项卡的代码放在一个类中,而是选择将每个选项卡放在不同的类中。现在,我在尝试将选项卡集成到应用程序主类的一个窗口时遇到了麻烦。这样做的最佳方法是什么?下面是其中一个类的示例,其中包含一个选项卡的代码,简化,其他类相对相同。这些课程本身很好。任何建议将不胜感激......
import java.awt.*;
import javax.swing.*;
public class calculate extends JFrame{
private static final long serialVersionUID = 1L;
public calculate () {
//GridBag Constraints
GridBagConstraints grid = new GridBagConstraints();
grid.fill = GridBagConstraints.BOTH;
grid.anchor = GridBagConstraints.NORTH;
//Create panel add components
JPanel p2 = new JPanel(new GridBagLayout());
JLabel floorOp = new JLabel("SELECT FLOORING OPTION", SwingConstants.CENTER);
grid.gridx = 0;
grid.gridy = 0;
grid.gridwidth = 2;
grid.ipady = 10;
grid.ipadx = 40;
floorOp.setOpaque(true);
floorOp.setBackground(Color.decode("#CCFFFF"));
floorOp.setBorder(BorderFactory.createMatteBorder(3, 0, 1, 0, Color.lightGray));
p2.add(floorOp, grid);
ButtonGroup groupBtn = new ButtonGroup();
JRadioButton smallBtn = new JRadioButton("Hard Wood");
groupBtn.add(smallBtn);
grid.gridx = 0;
grid.gridy = 1;
grid.gridwidth = 1;
grid.ipady = 10;
smallBtn.setOpaque(true);
smallBtn.setBackground(Color.WHITE);
smallBtn.setHorizontalAlignment(AbstractButton.CENTER);
p2.add(smallBtn, grid);
JRadioButton mediumBtn = new JRadioButton("Carpet");
groupBtn.add(mediumBtn);
grid.gridx = 1;
grid.gridy = 1;
mediumBtn.setOpaque(true);
mediumBtn.setBackground(Color.WHITE);
mediumBtn.setHorizontalAlignment(AbstractButton.CENTER);
p2.add(mediumBtn, grid);
JLabel space = new JLabel("");
grid.gridx = 0;
grid.gridy = 2;
grid.ipady = 25;
p2.add(space, grid);
JLabel dims = new JLabel("ENTER DIMENSIONS", SwingConstants.CENTER);
grid.gridx = 0;
grid.gridy = 3;
grid.gridwidth = 2;
grid.ipady = 10;
grid.ipadx = 40;
dims.setOpaque(true);
dims.setBackground(Color.decode("#CCFFFF"));
dims.setBorder(BorderFactory.createMatteBorder(3, 0, 1, 0, Color.lightGray));
p2.add(dims, grid);
JLabel length = new JLabel("Length:", SwingConstants.CENTER);
grid.gridx = 0;
grid.gridy = 4;
grid.gridwidth = 1;
length.setOpaque(true);
length.setBackground(Color.WHITE);
p2.add(length, grid);
JTextField lengthTxt = new JTextField(20);
grid.gridx = 1;
grid.gridy = 4;
p2.add(lengthTxt, grid);
JLabel width = new JLabel("Width:", SwingConstants.CENTER);
grid.gridx = 0;
grid.gridy = 5;
width.setOpaque(true);
width.setBackground(Color.WHITE);
p2.add(width, grid);
JTextField widthTxt = new JTextField(20);
grid.gridx = 1;
grid.gridy = 5;
p2.add(widthTxt, grid);
}
}
这是我认为我应该在我的主要做的事情,但它会抛出异常
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
public class appMain {
JFrame frame = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane();
selectTab fp = new selectTab();
calculate sp = new calculate();
public appMain() {
tabbedPane.add("First", fp);
tabbedPane.add("Second", sp);
frame.getContentPane().add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(640, 480);
// frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new appMain();
}
});
}
}