如何将背景图像添加到我的JFrame中以及U应该在哪里添加代码?
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon.*;
import javax.swing.*; //import java swing to use the Jframe etc
public class MathQuiz extends JFrame implements ActionListener
{
JCheckBox answer1 = new JCheckBox(); //checkbozx for answer 1
JCheckBox answer2 = new JCheckBox(); //checkbozx for answer 2
JCheckBox answer3 = new JCheckBox(); //checkbozx for answer 3
JTextArea textArea = new JTextArea();//text area for question
JButton exit = new JButton(); // button to exit quiz
JButton calculator = new JButton(); // button to open calculator
JLabel image = new JLabel(); //adds background image
// Some names which will be associated with numbers.
int answer;
int questionnumber;
/**
* This bit of code starts the process of showing the window.
**/
public MathQuiz()
{
// Drop down to the code labeled 'initComponents' below.
try {
initComponents();
} catch (Exception e) {}
}
/**
* This bit of code is the first bit that's run.
**/
static public void main(String[] args)
{
MathQuiz frame = new MathQuiz();
// The following code puts the Window on your screen.
frame.setVisible(true); //allows the jframe to be visible
frame.showQuestion(1);//shows the first question
frame.setSize(370, 350);//sets the size of the frame
}
/**
* This bit of code sets up how the Window looks.
**/
public void initComponents() throws Exception
{
编辑答案的复选框
answer1.setSize(new Dimension(80, 20)); //set size of answer 1 checkbox
answer1.setVisible(true); //sets visible to answer 1 checkbox
answer1.setMaximumSize(new Dimension(80, 20));
answer1.setMinimumSize(new Dimension(80, 20));
answer1.setLocation(new Point(10, 180)); //set location answer 1 checkbox
answer2.setSize(new Dimension(80, 20)); //set size of answer 2 checkbox
answer2.setVisible(true);//sets visible to answer 2 checkbox
answer2.setMaximumSize(new Dimension(80, 20));
answer2.setMinimumSize(new Dimension(80, 20));
answer2.setLocation(new Point(10, 210)); //set location answer 2 checkbox
answer3.setSize(new Dimension(80, 20)); //set size of answer 3 checkbox
answer3.setVisible(true);//sets visible to answer 3 checkbox
answer3.setMaximumSize(new Dimension(80, 20));
answer3.setMinimumSize(new Dimension(80, 20));
answer3.setLocation(new Point(10, 240)); //set location answer 3 checkbox
编辑JFrame
上的文本区域的代码 textArea.setSize(new Dimension(340, 80)); //set size of text ares
textArea.setLineWrap(true);
textArea.setVisible(true); //sets visible to text area
textArea.setMaximumSize(new Dimension(400, 140)); //set maximum size of text area
textArea.setMinimumSize(new Dimension(400, 140)); //set minimum size of text area
textArea.setLocation(new Point(10, 20)); //set location of text area
textArea.setEditable(false);
编辑JFrame
上的退出按钮的代码 exit.setSize(new Dimension(104, 40)); //set size of button
exit.setVisible(true); //sets visible to button
exit.setLocation(new Point(230, 180)); //set location of button
exit.setText("Exit");
编辑JFrame
上的退出按钮的代码 calculator.setSize(new Dimension(104, 40)); //set size of button
calculator.setVisible(true); //sets visible to button
calculator.setLocation(new Point(230, 230)); //set location of button
calculator.setText("Calculator");
image.setIcon(new ImageIcon("maths"));
image.setVisible(true);
这将设置Window的大小并将swing实用程序添加到
setSize(new Dimension(430, 292));
setResizable(false);
getContentPane().setLayout(null);
setTitle("Multiple Choice Math Quiz!");
setLocation(new Point(200, 200));
getContentPane().add(answer1);
getContentPane().add(answer2);
getContentPane().add(answer3);
getContentPane().add(textArea);
getContentPane().add(exit);
getContentPane().add(calculator);
答案 0 :(得分:0)