2017-12-06 16 views
0

私は誰かがこれで正しい方向に私をプッシュできることを願っています。私は3つの別々のクラス、Calculator,Calculator2、およびCalculator3を持っています。基本的にはボタンのいくつかをいくつか変更した点を除いては同じですので、Calculatorのコードを貼り付けます。私はどうすればそれらを得ることができるのだろうかと疑問に思っていたので、すべてが1つのJFrameの中でメインの隣に表示されますか?私は最近のメインの試みも添付しました。ここで1つのJFrameに1つ以上のオブジェクトがあります

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

答えて

0

あなただけの単一のJFrameを作成し、単一JPanel.Don'tにあなたの電卓クラスごとに置く必要があります各クラスの新しい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); 
    } 
} 
関連する問題