GUI 编程 - 自定义框架
GUI Programming - Customizing frame
我刚刚开始使用 GUI 编程。我正在尝试自定义框架,但是当我将它添加到 gameView 时,到目前为止没有出现任何内容,例如按钮或猫 gif,所以我得到的只是 window 在我 运行 之后的框架。到目前为止,这是我的代码:
package experiment10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mainFrame extends JFrame
{
public static int MAX = 500; // maximum number to guess
private JButton submitBtn; // Submit button
private JButton clearBtn; // Clear Input button
private JTextField inputNumber; //input guessed number
private int magicNumber; //randomly generated number
public static void main(String[] arg) {
//Show frame
JFrame frame = new JFrame();
frame.setVisible(true);
}
public mainFrame() {
setSize(400, 400);
setResizable (false);
setTitle("Let's Play Hi Lo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
magicNumber = (int)(Math.random()*MAX);
Container gameView = getContentPane();
gameView.setLayout(new FlowLayout());
//Customizations
submitBtn = new JButton("Submit"); //create submit button
clearBtn = new JButton("Clear"); //create clear button
gameView.add(submitBtn); //adds submit button
gameView.add(clearBtn); //adds clear button
JLabel imageLabel = new JLabel(new ImageIcon("cat.gif")); //creates image
gameView.add(imageLabel); //adds image
JLabel textLabel = new JLabel("Please enter your guess"); //creates JLabel
gameView.add(textLabel); //adds JLabel
JTextField input = new JTextField(); //creates JTextField
gameView.add(input); //adds JTextField
}
}
任何 help/tips 非常感谢。谢谢。
您需要从主方法调用 mainFrame():
mainFrame frame = new mainFrame();
您还可以从 main 中删除以下代码:
JFrame frame = new JFrame();
frame.setVisible(true);
然后您需要将 setVisible(true);
添加到您的 mainFrame() 构造函数中。
我刚刚开始使用 GUI 编程。我正在尝试自定义框架,但是当我将它添加到 gameView 时,到目前为止没有出现任何内容,例如按钮或猫 gif,所以我得到的只是 window 在我 运行 之后的框架。到目前为止,这是我的代码:
package experiment10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mainFrame extends JFrame
{
public static int MAX = 500; // maximum number to guess
private JButton submitBtn; // Submit button
private JButton clearBtn; // Clear Input button
private JTextField inputNumber; //input guessed number
private int magicNumber; //randomly generated number
public static void main(String[] arg) {
//Show frame
JFrame frame = new JFrame();
frame.setVisible(true);
}
public mainFrame() {
setSize(400, 400);
setResizable (false);
setTitle("Let's Play Hi Lo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
magicNumber = (int)(Math.random()*MAX);
Container gameView = getContentPane();
gameView.setLayout(new FlowLayout());
//Customizations
submitBtn = new JButton("Submit"); //create submit button
clearBtn = new JButton("Clear"); //create clear button
gameView.add(submitBtn); //adds submit button
gameView.add(clearBtn); //adds clear button
JLabel imageLabel = new JLabel(new ImageIcon("cat.gif")); //creates image
gameView.add(imageLabel); //adds image
JLabel textLabel = new JLabel("Please enter your guess"); //creates JLabel
gameView.add(textLabel); //adds JLabel
JTextField input = new JTextField(); //creates JTextField
gameView.add(input); //adds JTextField
}
}
任何 help/tips 非常感谢。谢谢。
您需要从主方法调用 mainFrame():
mainFrame frame = new mainFrame();
您还可以从 main 中删除以下代码:
JFrame frame = new JFrame();
frame.setVisible(true);
然后您需要将 setVisible(true);
添加到您的 mainFrame() 构造函数中。