运行代码时的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);
}
}
答案 0 :(得分:1)
好的,你的代码的问题在于你没有使用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();
}
}