我已經開始創建一個由幾個選項卡組成的GUI。現在我專注於其中兩個。池選項卡和熱浴盆選項卡。當我第一次開始時,我讓所有的東西在游泳池選項卡上工作得很好。所以我想,因爲所有的標籤和文本框放置將是相同的熱浴缸選項卡我只是複製編碼。好吧,我這樣做了,試着將所有標籤和文本框命名爲與之後的數字2相同。它不工作。現在Hot Tub選項卡可以工作,但Pool選項卡不可用,並且文本框不再顯示。我也有文本框的對齊問題,但我認爲這與標籤和文本框的命名有關,我不確定。文本框和標籤問題
主類:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
public class test extends JFrame implements ActionListener{
private JTabbedPane jtabbedPane;
private JPanel General;
private JPanel Pools;
private JPanel HotTub;
JTextField lengthText, widthText, depthText, volumeText;
public test(){
setTitle("Volume Calculator");
setSize(300, 200);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
createGeneral();
createPools();
jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("General", General);
jtabbedPane.addTab("Pool", Pools);
jtabbedPane.addTab("Hot Tub", HotTub);
topPanel.add(jtabbedPane, BorderLayout.CENTER);
}
public void createGeneral(){
General = new JPanel();
General.setLayout(null);
JLabel dateLabel = new JLabel("Today's Date");
dateLabel.setBounds(10, 15, 150, 20);
General.add(dateLabel);
JFormattedTextField date = new JFormattedTextField(
java.util.Calendar.getInstance().getTime());
date.setEditable(false);
date.setBounds(90,15,150,20);
General.add(date);
JButton Close = new JButton("Close");
Close.setBounds(20,50,80,20);
Close.addActionListener(this);
Close.setBackground(Color.white);
General.add(Close);
}
/* CREATE POOL */
public void createPools(){
Pools = new JPanel();
Pools.setLayout(null);
JLabel lengthLabel = new JLabel("Length of pool (ft):");
lengthLabel.setBounds(10, 15, 260, 20);
Pools.add(lengthLabel);
lengthText = new JTextField();
lengthText.setBounds(260, 15, 150, 20);
Pools.add(lengthText);
JLabel widthLabel = new JLabel("Width of pool (ft):");
widthLabel.setBounds(10, 60, 260, 20);
Pools.add(widthLabel);
widthText = new JTextField();
widthText.setBounds(260, 60, 150, 20);
Pools.add(widthText);
JLabel depthLabel = new JLabel("Average Depth of pool (ft):");
depthLabel.setBounds(10, 100, 260, 20);
Pools.add(depthLabel);
depthText = new JTextField();
depthText.setBounds(260, 100, 150, 20);
Pools.add(depthText);
JLabel volumeLabel = new JLabel("The pool's volume is:(ft ^3");
volumeLabel.setBounds(10, 200, 260, 20);
Pools.add(volumeLabel);
volumeText = new JTextField();
volumeText.setBounds(260, 200, 150, 20);
volumeText.setEditable(false);
Pools.add(volumeText);
JButton calcVolume = new JButton("Calculate Volume");
calcVolume.setBounds(150,250,150,20);
calcVolume.addActionListener(this);
calcVolume.setBackground(Color.white);
Pools.add(calcVolume);
JButton Close = new JButton("Close");
Close.setBounds(350,250,80,20);
Close.addActionListener(this);
Close.setBackground(Color.white);
Pools.add(Close);
}
public void actionPerformed(ActionEvent event){
JButton button = (JButton)event.getSource();
String buttonLabel = button.getText();
if ("Close".equalsIgnoreCase(buttonLabel)){
Exit_pressed(); return;
}
if ("Calculate Volume".equalsIgnoreCase(buttonLabel)){
Calculate_Volume(); return;
}
if ("Calculate Volume".equalsIgnoreCase(buttonLabel)){
Calculate_Volume(); return;
}
}
private void Exit_pressed(){
System.exit(0);
}
private void Calculate_Volume(){
String lengthString, widthString, depthString;
int length=0;
int width=0;
int depth=0;
lengthString = lengthText.getText();
widthString = widthText.getText();
depthString = depthText.getText();
if (lengthString.length() < 1 || widthString.length() < 1 || depthString.length() < 1){
volumeText.setText("Enter All 3 Numbers"); return;
}
length = Integer.parseInt(lengthString);
width = Integer.parseInt(widthString);
depth = Integer.parseInt(depthString);
if (length != 0 || width != 0 || depth != 0){
volumeText.setText((length * width * depth) + "");
} else{
volumeText.setText("Enter All 3 Numbers"); return;
}
}
public static void main(String[] args){
JFrame frame = new test();
frame.setSize(500, 350);
frame.setVisible(true);
}
}
熱水浴缸類:
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public abstract class HotTub extends JFrame implements ActionListener{
private JTabbedPane jtabbedPane;
private Component HotTub;
{
jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("Hot Tub", HotTub);
JPanel HotTub;
JTextField lengthText, widthText, depthText, volumeText;
/* CREATE HOT TUB */
HotTub = new JPanel();
HotTub.setLayout(null);
JLabel lengthLabel = new JLabel("Length of hot tub (ft):");
lengthLabel.setBounds(10, 15, 260, 20);
HotTub.add(lengthLabel);
lengthText = new JTextField();
lengthText.setBounds(260, 15, 150, 20);
HotTub.add(lengthText);
JLabel widthLabel = new JLabel("Width of hot tub (ft):");
widthLabel.setBounds(10, 60, 260, 20);
HotTub.add(widthLabel);
widthText = new JTextField();
widthText.setBounds(260, 60, 150, 20);
HotTub.add(widthText);
JLabel depthLabel = new JLabel("Average Depth of hot tub (ft):");
depthLabel.setBounds(10, 100, 260, 20);
HotTub.add(depthLabel);
depthText = new JTextField();
depthText.setBounds(260, 100, 150, 20);
HotTub.add(depthText);
JLabel volumeLabel = new JLabel("The hot tub's volume is:(ft ^3");
volumeLabel.setBounds(10, 200, 260, 20);
HotTub.add(volumeLabel);
volumeText = new JTextField();
volumeText.setBounds(260, 200, 150, 20);
volumeText.setEditable(false);
HotTub.add(volumeText);
JButton calcVolume = new JButton("Calculate Volume");
calcVolume.setBounds(150,250,150,20);
calcVolume.addActionListener((ActionListener) this);
calcVolume.setBackground(Color.white);
HotTub.add(calcVolume);
JButton Close = new JButton("Close");
Close.setBounds(350,250,80,20);
Close.addActionListener((ActionListener) this);
Close.setBackground(Color.white);
HotTub.add(Close);
}
}
眼下池選項卡和熱水浴池選項卡都是相同的。無論我使用哪個標籤,每個標籤上都會顯示相同的結果。這是一個命名問題嗎?
有很多代碼要穿過這裏 – 2011-03-17 12:42:29
請問您能否嘗試刪除一些代碼,並且只包含相關位? – jjnguy 2011-03-17 12:46:07
我刪除了一些,並將課程分開,但我認爲它不對。請看我原來的帖子。謝謝 – Mike 2011-03-17 13:31:30