我正在做一个简单的真或假测验游戏,但我不知道我应该使用哪个文字字段来显示测验问题。我尝试了JTextField但你可以编辑它中的文本..我只想问题字符串从问题类中获取并显示在文本区域中。
测验课
public class Quiz
{
private JFrame frame;
private JButton yesButton;
private JButton noButton;
/**
* Creates game interface
*/
public Quiz()
{
makeFrame();
}
/**
* Receive notification of an action.
*/
public void actionPerformed(ActionEvent event)
{
System.out.println("Menu item: " + event.getActionCommand());
}
/**
* Quits the application.
*/
private void quit()
{
System.exit(0);
}
/**
* About pop up.
*/
private void about()
{
JOptionPane.showMessageDialog(frame,
"Quiz game version 1.0",
"About Quiz",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* About pop up.
*/
private void howToPlay()
{
JOptionPane.showMessageDialog(frame,
"This is a simple quiz game where you answer questions by pressing yes or no buttons",
"How to Play",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Enable's or disable's buttons.
*/
private void setButtonsEnabled(boolean status)
{
yesButton.setEnabled(status);
noButton.setEnabled(status);
}
/**
* Creates the frame and its content.
*/
private void makeFrame()
{
frame = new JFrame("Quiz");
Container contentPane = frame.getContentPane();
makeMenuBar(frame);
//JPanel
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 0));
yesButton = new JButton("True");
panel.add(yesButton);
noButton = new JButton("False");
panel.add(noButton);
contentPane.add(panel,BorderLayout.CENTER);
// end of JPanel
//score
JLabel label = new JLabel("Score: ");
contentPane.add(label, BorderLayout.NORTH);
//end of score
//combo box
JComboBox noOfPlayers = new JComboBox();
contentPane.add(noOfPlayers, BorderLayout.SOUTH);
noOfPlayers.addItem("1 Player");
noOfPlayers.addItem("2 Player");
noOfPlayers.addItem("3 Player");
noOfPlayers.addItem("4 Player");
noOfPlayers.addItem("5 Player");
// end of combo box
//
//
//Shows the frame and places frame at the center of the screen
frame.pack();
setButtonsEnabled(false);
//makes application start in the center of the screen
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
答案 0 :(得分:3)
如果您致电setEditable(false)
,则无法在JTextField中修改文字。如果您也在其上调用setFocusable(false)
,则甚至无法对其进行制表。
修改强>
你问:
我在哪里设置它?因为我刚试过JTextField qText = new JTextField(setEditable(false));但它不工作..或者我必须制作一个新方法,然后调用该方法?
您的qText字段应该是私有实例字段。然后你可以调用你的Quiz类的构造函数中的方法。
答案 1 :(得分:1)
你可以设置属性textfield.Editable = false - >用户无法编辑