java - 文本框和标签问题

标签 java swing textbox label

我已经开始创建一个由几个选项卡组成的 GUI。现在我专注于其中两个。 “池”选项卡和“热水浴缸”选项卡。当我第一次开始时,我在池选项卡上一切正常。所以我想,由于“热水浴缸”选项卡的所有标签和文本框位置都是相同的,我只需复制代码即可。好吧,我这样做了,并尝试将所有标签和文本框命名为相同的名称,只是在它们后面加上数字 2。它不起作用。现在“热水浴缸”选项卡可以使用,但“泳池”选项卡不能使用,而且文本框也消失了。我也遇到了文本框的对齐问题,但我认为这与标签和文本框的命名有关,我不确定。

主要类(class):

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);
                           }
}

现在“泳池”选项卡和“热水浴缸”选项卡是相同的。无论我在哪个选项卡上,每个选项卡上都会显示相同的结果。是命名问题吗?

最佳答案

  1. 这不应该全部在一个类中。
  2. 如果您的泳池和热水浴缸选项卡非常相似,以至于您正在复制代码。相反,创建一个扩展 JPanel 的新类并根据一些参数设置面板。 (甚至只是一个工厂方法。)然后将其中两个类添加到 JTabbedPane,一个包含 HotTub 的参数,另一个包含 Pool 的参数。
  3. 使用布局管理器。学习曲线是值得的,并且会极大地改善你的 GUI。

关于java - 文本框和标签问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5339037/

相关文章:

java - 如何模拟 lambda 表达式 anymatch 返回对象

java - 如何在 junit 中使用未捕获的异常处理程序进行多线程测试?

java - 通过重采样调整 JPanel 的大小以保留内容

java - GridLayout不通过按钮点击添加组件?

c# - 在文本框上显示建议

java - 如何获取 java 反射方法对象的 `signature` 字段

java - Maven clean 无法删除文件

java - JComboBox 列出年龄

javascript - 在 iframe 中的光标位置插入 HTML 控件

c# - WPF 绑定(bind)到文本框不更新