单个JFrame中有多个对象

时间:2017-12-06 02:00:03

标签: java swing jframe

我希望有人可以用这个推动我朝着正确的方向前进。我有3个单独的类,CalculatorCalculator2Calculator3。原则上它们都是相同的,除了对某些按钮进行一些更改之外,所以我只是粘贴Calculator的代码。我想知道我怎样才能得到它们所以所有出现在一个主要的彼此相邻的JFrame中?我附上了我最近的主要尝试。

以下是Calculator

public class Calculator implements ActionListener {
private JFrame frame;
private JTextField xfield, yfield;
private JLabel result;
private JButton subtractButton;
private JButton divideButton;
private JButton addButton;
private JButton timesButton;
private JPanel xpanel;

public Calculator() {      
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    xpanel = new JPanel();
    xpanel.setLayout(new GridLayout(3,2));

    xpanel.add(new JLabel("x:", SwingConstants.RIGHT));
    xfield = new JTextField("0", 5);
    xpanel.add(xfield);

    xpanel.add(new JLabel("y:", SwingConstants.RIGHT));
    yfield = new JTextField("0", 5);
    xpanel.add(yfield);

    xpanel.add(new JLabel("Result:"));
    result = new JLabel("0");
    xpanel.add(result);
    frame.add(xpanel, BorderLayout.NORTH);

    /***********************************************************************
     *********************************************************************** 
     **********************************************************************/

    JPanel southPanel = new JPanel();                      //New panel for the artimatic buttons
    southPanel.setBorder(BorderFactory.createEtchedBorder());

    timesButton = new JButton("Multiplication");
    southPanel.add(timesButton);
    timesButton.addActionListener(this);

    subtractButton = new JButton("Subtract");
    southPanel.add(subtractButton);
    subtractButton.addActionListener(this);

    divideButton = new JButton("Division");
    southPanel.add(divideButton);
    divideButton.addActionListener(this);

    addButton = new JButton("Addition");
    southPanel.add(addButton);
    addButton.addActionListener(this);

    frame.add(southPanel , BorderLayout.SOUTH);

    Font thisFont = result.getFont();                                       //Get current font
    result.setFont(thisFont.deriveFont(thisFont.getStyle() ^ Font.BOLD));   //Make the result bold
    result.setForeground(Color.red);                                        //Male the result answer red in color
    result.setBackground(Color.yellow);                                     //Make result background yellow
    result.setOpaque(true);

    frame.pack();
    frame.setVisible(true);
}
/**
 * clear()
 * Resets the x and y field to 0 after invalid integers were input
 */
public void clear() {
    xfield.setText("0");
    yfield.setText("0");
}

@Override
public void actionPerformed(ActionEvent event) { 
    String xText = xfield.getText();                        //Get the JLabel fiels and set them to strings
    String yText = yfield.getText();

    int xVal;
    int yVal;

    try {
        xVal = Integer.parseInt(xText);                     //Set global var xVal to incoming string
        yVal = Integer.parseInt(yText);                     //Set global var yVal to incoming string
    }
    catch (NumberFormatException e) {                       //xVal or yVal werent valid integers, print message and don't continue
        result.setText("ERROR");
        clear();
        return ;
    } 

    if(event.getSource().equals(timesButton)) {             //Button pressed was multiply
        result.setText(Integer.toString(xVal*yVal)); 
    }
    else if(event.getSource().equals(divideButton)) {       //Button pressed was division
        if(yVal == 0) {                                   //Is the yVal (bottom number) 0?
            result.setForeground(Color.red);              //Yes it is, print message
            result.setText("CAN'T DIVIDE BY ZERO!");
            clear();
        }
        else          
            result.setText(Integer.toString(xVal/yVal));  //No it's not, do the math
    }
    else if(event.getSource().equals(subtractButton)) {    //Button pressed was subtraction                                          
        result.setText(Integer.toString(xVal-yVal)); 
    }
    else if(event.getSource().equals(addButton)) {        //Button pressed was addition
        result.setText(Integer.toString(xVal+yVal)); 
    }
  }
}

这是我目前的主要内容:

public class DemoCalculator {
public static void main(String[] args) {
    JFrame mainFrame = new JFrame("Calculators");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Calculator calc = new Calculator();
    Calculator2 calc2 = new Calculator2();
    JPanel calcPanel = new JPanel(new BorderLayout());
    JPanel calcPanel2 = new JPanel(new BorderLayout());

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    //calcPanel.add(calc, BorderLayout.CENTER);
    //calcPanel2.add(calc2, BorderLayout.CENTER);
    mainPanel.add(calcPanel);
    mainPanel.add(calcPanel2);
    calcPanel.add(mainPanel);

    mainFrame.getContentPane().add(calcPanel);
    mainFrame.getContentPane().add(calcPanel2);
    mainFrame.pack();
    mainFrame.setVisible(true); 
    }
}

1 个答案:

答案 0 :(得分:0)

您应该只创建一个JFrame并将每个计算器类放入一个JPanel.Don为每个类创建一个新的JFrame。

public class Calculator{ 

JPanel panel;
public Calculator(){

  panel = new JPanel();

  /*
  *  Add labels and buttons etc.
  */
   panel.add(buttons);
   panel.add(labels);
}

  //Method to return the JPanel
  public JPanel getPanel(){
  return panel;
 }
}

然后使用适合您需要的任何布局将面板添加到测试人员类的JFrame中。

public class DemoCalculator {

public static void main(String[] args) {

    JPanel cal1,cal2;
    JFrame frame = new JFrame("Calculators");

    frame.add(cal1 = new Calculator().getPanel(),new BorderLayout().EAST);
    frame.add(cal2 = new Calculator2().getPanel(),new BorderLayout().WEST);

    frame.setSize(1000,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
  } 
}