我希望有人能用勺子喂我这个解决方案。它是我班级主要实验室的一部分,它实际上并没有给我太多,因为我只是不了解如何使用制表符制作GUI。我可以使用某种GUI制作常规程序,但我一直在搜索和阅读,因为整个类部分和声明私有变量,所以不能放2和2。所以我要问的是,如果有人可以让我成为一个带有5个选项卡的主GUI,并将我的代码放入1个选项卡中,这样我就可以学习并将其余的代码放入其他4个选项卡中。所以我希望你不要以为我想要你在代码时给我代码,我只是没有真正得到标签,我们还没有在课堂上讨论它。这是带有自己gui的代码,我希望我输入的内容很有意义。我通过观察学习代码,这将帮助我一堆。
package landscape;
import javax.swing.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Landscape extends JFrame {
private JFrame mainFrame;
private JButton calculateButton;
private JButton exitButton;
private JTextField lengthField;
private JLabel lengthLabel;
private JTextField widthField;
private JLabel widthLabel;
private JTextField depthField;
private JLabel depthLabel;
private JTextField volumeField;
private JLabel volumeLabel;
private JRadioButton builtIn;
private JRadioButton above;
private JTabbedPane panel;
public Landscape()
{
JTabbedPane tabs=new JTabbedPane();
tabs.addTab("Pool", panel);//add a tab for the panel with the title "title"
//you can add more tabs in the same fashion - obviously you can change the title
//tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab
mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane
mainFrame.setSize(265,200);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
mainFrame.setResizable(false);
JPanel panel = new JPanel();//FlowLayout is default
//Pool
panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel
panel.add(builtIn);
panel.add(above);
panel.add(lengthLabel);
panel.add(lengthField);
panel.add(widthLabel);
panel.add(widthField);
panel.add(depthLabel);
panel.add(depthField);
panel.add(volumeLabel);
panel.add(volumeField);
panel.add(calculateButton);
panel.add(exitButton);
//creating components
calculateButton = new JButton ("Calculate");
exitButton = new JButton ("Exit");
lengthField = new JTextField (5);
lengthLabel = new JLabel ("Enter the length of your pool: ");
widthField = new JTextField (5);
widthLabel = new JLabel ("Enter the width of your pool: ");
depthField = new JTextField (5);
depthLabel = new JLabel ("Enter the depth of your pool: ");
volumeField = new JTextField (5);
volumeLabel = new JLabel ("Volume of the pool: ");
//radio button
ButtonGroup buttonGroup = new ButtonGroup();
builtIn = new JRadioButton ("Built in");
buttonGroup.add(builtIn);
above = new JRadioButton ("Above");
buttonGroup.add(above);
exitButton.setMnemonic('x');
calculateButton.setMnemonic('C');
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
// create the handlers
calculateButtonHandler chandler = new calculateButtonHandler(); //instantiate new object
calculateButton.addActionListener(chandler); // add event listener
ExitButtonHandler ehandler = new ExitButtonHandler();
exitButton.addActionListener(ehandler);
FocusHandler fhandler = new FocusHandler();
lengthField.addFocusListener(fhandler);
widthField.addFocusListener(fhandler);
depthField.addFocusListener(fhandler);
}
class calculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat num = new DecimalFormat(", ###.##");
double width;
double length;
double depth;
double volume;
double volume2;
double height;
String instring;
instring = lengthField.getText();
if (instring.equals(""))
{
instring = "0";
lengthField.setText("0");
}
length = Double.parseDouble(instring);
instring = widthField.getText();
if (instring.equals(""))
{
instring = "0";
widthField.setText("0");
}
width = Double.parseDouble(instring);
instring = depthField.getText();
if (instring.equals(""))
{
instring = "0";
depthField.setText("0");
}
depth = Double.parseDouble(instring);
volume = width * length * depth;
volumeField.setText(num.format(volume));
volume2 = width * length * depth;
}
}
class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class FocusHandler implements FocusListener
{
public void focusGained(FocusEvent e)
{
if(e.getSource() == lengthField || e.getSource() == widthField || e.getSource() == depthField)
{
volumeField.setText("");
}
else if (e.getSource() == volumeField)
{
volumeField.setNextFocusableComponent(calculateButton);
calculateButton.grabFocus();
}
}
public void focusLost1(FocusEvent e)
{
if(e.getSource() == widthField)
{
widthField.setNextFocusableComponent(calculateButton);
}
}
public void focusLost(FocusEvent e)
{
if(e.getSource() == depthField)
{
depthField.setNextFocusableComponent(calculateButton);
}
}
}
public static void main(String args[])
{
Landscape app = new Landscape();
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
不是获取内容窗格并添加到内容窗格,而是创建一个新的JPanel
并添加您的内容。然后,将面板添加到具有所需标题的新JTabbedPane
,并将JFrame
的内容窗格设置为选项卡式窗格。
以下是您将要做的一个简单示例:
JPanel panel=new JPanel();//FlowLayout is default
panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel
panel.add(builtIn);
panel.add(above);
//...you get the picture; add all the stuff you already do, just use panel instead of c
JTabbedPane tabs=new JTabbedPane();
tabs.addTab("title", panel);//add a tab for the panel with the title "title"
//you can add more tabs in the same fashion - obviously you can change the title
tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab
mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane
您可以保留代码的其余部分,它应该可以正常工作。