运行代码时的ScreenShot


当我运行代码时,okButton不会遵循我的尺寸,而是显示在全屏上。

另外,我注意到,当光标位于okButton方法内部时,按钮似乎符合我的尺寸。

出了什么问题?

import java.awt.*;

public class WindowGui extends Frame {

public static TextArea tx ;
public static Button okButton;

public WindowGui(){

    Window();

    TextArea();

    okButton();

    this.addWindowListener(new CloseWindowAndExit());
}

public void Window(){
    //this.setExtendedState(Frame.MAXIMIZED_BOTH);
    this.setSize(1600,900);
    this.setTitle("LogicGate Simulator");
    this.setBackground(Color.BLUE);
    this.toFront();
    this.setVisible(true);

    System.out.println("kamerja");
}

public void TextArea(){

    tx = new TextArea("kalimeres",2,30);

    tx.setText("Σύντομες Οδηγίες LogicGate Simulator");
    tx.setEditable(false);
    tx.setForeground(Color.BLACK);
    tx.setEditable(false);
    this.add(tx);

    System.out.println("kaliSSmerja");
}

public void okButton(){
    okButton = new Button ("OK");
    okButton.setSize(new Dimension(50,50));
    okButton.setLocation(500, 350);
    okButton.setFont(new Font ("Times New Roman",Font.PLAIN,14));
    this.add(okButton);
}
}

最佳答案

好的,所以代码的问题是您没有使用JFrame创建框架,这样做可以使代码正常工作(我已经编辑了代码并将其发布在下面!)。

主要代码:

import java.awt.*;
import javax.swing.JFrame;

public class NewClass extends Frame {

public static TextArea tx ;
public static Button okButton;
public static JFrame frame;

public static void NewClass(){

Window();

TextArea();

okButton();


}

public static void Window(){
//this.setExtendedState(Frame.MAXIMIZED_BOTH);
frame = new JFrame();
frame.setSize(1600,900);
frame.setTitle("LogicGate Simulator");
frame.setBackground(Color.BLUE);
frame.toFront();
frame.setVisible(true);

System.out.println("kamerja");
}

public static void TextArea(){

tx = new TextArea("kalimeres",2,30);

tx.setText("Σύντομες Οδηγίες LogicGate Simulator");
tx.setEditable(false);
tx.setForeground(Color.BLACK);
tx.setEditable(false);
frame.add(tx);

System.out.println("kaliSSmerja");
}

public static void okButton(){
okButton = new Button ("OK");
okButton.setSize(new Dimension(50,50));
okButton.setLocation(500, 350);
okButton.setFont(new Font ("Times New Roman",Font.PLAIN,14));
frame.add(okButton);
}
}


运行代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StackOverflowHelpAnswers extends NewClass{
public static class okButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){

}
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    //do something
    NewClass();
}
}

10-07 16:53
查看更多