我的UI在同一窗口中显示两次

时间:2015-10-26 00:35:45

标签: java

Here's an example

我的Jtextfields和Jbuttons在同一个窗口中被复制,并且看起来功能完全相同。

这可能是一个简单的解决方法,但是你可以说我在编码方面非常糟糕。

(哦和变量的一些名称等等是占位符:p)

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.FlowLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JTextField;
import javax.swing.JLabel;

public class Adding extends JFrame {
  public Adding(Heavy_Lifting lifting) {
    addUI(lifting);
  }

  public void addUI(final Heavy_Lifting lifting) {
    setLayout(new FlowLayout());

    JButton addButton = new JButton("Enter");
    JButton backButton = new JButton("Quit");
    final JTextField eInput = new JTextField("Enter english name");
    final JTextField mInput = new JTextField("Enter maori name");
    final JTextField dInput = new JTextField("Enter description");

    //add(addButton);
    //add(eInput);
    //add(mInput);
    //add(backButton);
    //add(dInput);

    Dimension x = new Dimension(500, 50);

    //addButton.setText("Enter");
    addButton.setPreferredSize(x);

    //backButton.setText("Quit");
    backButton.setPreferredSize(x);

    //eInput.setText("Enter english name");
    eInput.setPreferredSize(x);

    //mInput.setText("Enter maori name");
    mInput.setPreferredSize(x);

    //dInput.setText("Enter description");
    dInput.setPreferredSize(x);

    add(addButton);
    add(eInput);
    add(mInput);
    add(dInput);
    add(backButton);

    addButton.addActionListener(new ActionListener() {@
      Override
      public void actionPerformed(ActionEvent e) {
        String mname = mInput.getText();
        String ename = eInput.getText();
        String desc = dInput.getText();
        PeePee p = new PeePee(mname);
        Description d = new Description(desc);
        if (lifting.allChar(ename, p)) {
          lifting.insert(ename, p);
          lifting.insert(ename, d);
          eInput.setText("1");
          mInput.setText("2");
          dInput.setText("3");
        } else {
          eInput.setText("4");
          mInput.setText("5");
          dInput.setText("6");
        }
      }
    });

    backButton.addActionListener(new ActionListener() {@
      Override
      public void actionPerformed(ActionEvent event) {
        setVisible(false);
      }
    });

    setTitle("placeholder");
    setSize(550, 300);
    setMinimumSize(new Dimension(550, 300));
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  }
}

1 个答案:

答案 0 :(得分:0)

这看起来像您的公共方法addUI被调用两次,可能是另一个类。尝试将其切换为私有,看看它是否仍然运行并产生相同的视觉输出。