你好,我有一些问题,我想创建一个问题,当我单击play game我想添加一个JPanel到我的JFrame GameGUI和替换按钮的名称,并设置一个问题,当我单击in按钮,我通过下一个问题(quizz:/)但JPanel没有显示在我的JFrame中。对不起我的英语:'(
下面是Main类的代码:
public class Module extends JFrame implements DocumentListener {
private static boolean run;
public static ArrayList <Question> QuestionList = new ArrayList <Question>();
public static ArrayList <QuizzF> PanelList = new ArrayList <QuizzF>();
public static GUI GameGUI = new GUI();
public static JPanel content = new JPanel();
public static final CardLayout card = new CardLayout();
public Module() {
initComponents();
jTextField1.getDocument().addDocumentListener(this);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel2 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jButton5 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("QuizzWinner - Categorie");
setMinimumSize(new java.awt.Dimension(600, 488));
setResizable(false);
getContentPane().setLayout(null);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("CONNAISANCE");
getContentPane().add(jLabel2);
jLabel2.setBounds(110, 228, 110, 80);
jButton2.setText("PLAY");
jButton2.setEnabled(false);
getContentPane().add(jButton2);
jButton2.setBounds(435, 310, 73, 23);
jButton3.setText("PLAY");
jButton3.setEnabled(false);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(110, 140, 73, 23);
jTextField1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
jTextField1.setMinimumSize(new java.awt.Dimension(5, 10));
getContentPane().add(jTextField1);
jTextField1.setBounds(20, 420, 160, 20);
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("SPORT");
getContentPane().add(jLabel3);
jLabel3.setBounds(290, 70, 80, 60);
jLabel8.setForeground(new java.awt.Color(255, 255, 255));
jLabel8.setText("Your Name :");
getContentPane().add(jLabel8);
jLabel8.setBounds(20, 390, 120, 30);
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("MEDCINE");
getContentPane().add(jLabel4);
jLabel4.setBounds(285, 218, 80, 100);
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("CULTURE GENERAL");
getContentPane().add(jLabel5);
jLabel5.setBounds(420, 228, 130, 80);
jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel6.setForeground(new java.awt.Color(255, 255, 255));
jLabel6.setText("CINEMA");
getContentPane().add(jLabel6);
jLabel6.setBounds(450, 40, 90, 110);
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel7.setForeground(new java.awt.Color(255, 255, 255));
jLabel7.setText("ETUDE");
getContentPane().add(jLabel7);
jLabel7.setBounds(130, 70, 80, 60);
jButton5.setText("PLAY");
jButton5.setEnabled(false);
getContentPane().add(jButton5);
jButton5.setBounds(435, 140, 73, 23);
jButton4.setText("PLAY");
jButton4.setEnabled(false);
getContentPane().add(jButton4);
jButton4.setBounds(273, 140, 73, 23);
jButton6.setText("PLAY");
jButton6.setEnabled(false);
getContentPane().add(jButton6);
jButton6.setBounds(110, 310, 73, 23);
jButton7.setText("PLAY");
jButton7.setEnabled(false);
getContentPane().add(jButton7);
jButton7.setBounds(273, 310, 73, 23);
jLabel1.setIcon(new javax.swing.ImageIcon("jk.jpg")); // NOI18N
getContentPane().add(jLabel1);
jLabel1.setBounds(0, 0, 600, 460);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
content.setLayout(card);
addToArray("QuestionBank.txt");
buildPanel();
playGamelopp();
System.exit(0);
}
@Override
public void insertUpdate(DocumentEvent e) {
if(jTextField1.getText().length() >= 5){
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
jButton6.setEnabled(true);
jButton7.setEnabled(true);
}
}
@Override
public void removeUpdate(DocumentEvent e) {
if(jTextField1.getText().length() < 5){
jButton2.setEnabled(false);
jButton3.setEnabled(false);
jButton4.setEnabled(false);
jButton5.setEnabled(false);
jButton6.setEnabled(false);
jButton7.setEnabled(false);
}
}
private static void addToArray(String Lien) {
try {
FileReader QuestionFile = new FileReader (Lien);
BufferedReader Reader = new BufferedReader(QuestionFile);
FileReader LineReader = new FileReader(Lien);
LineNumberReader LineNumber = new LineNumberReader(LineReader);
LineNumber.skip(Long.MAX_VALUE); // Not sure why this line here; ask someone
int numQuestions = (LineNumber.getLineNumber() + 1) / 6; // +1 because it starts counting at 0, divide by 6 because 6 lines per question
LineNumber.close();
for (int j=0; j < numQuestions; j++){
Question NewQuestion = new Question(Reader.readLine(), Reader.readLine(), Reader.readLine(), Reader.readLine(), Reader.readLine(), Reader.readLine());
QuestionList.add(NewQuestion);
}
} catch (IOException e){
JOptionPane.showMessageDialog(null, "We have encountered an error. Please confirm that the QuestionFile has not been removed from the game directory.\n\n");
System.exit(0);
}
}
private static void buildPanel() {
for (int i=0; i < QuestionList.size(); ++i) {
Question Q = QuestionList.get(i);
QuizzF NewPanel = new QuizzF (Q.getQuestion(), Q.getOptionA(), Q.getOptionB(),Q.getOptionC(), Q.getOptionD(), Q.getCorrectOption());
PanelList.add(NewPanel);
content.add(NewPanel, Integer.toString(i));
}
GameGUI.setVisible(true);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Module.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(Module::new);
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JTextField jTextField1;
// End of variables declaration
@Override
public void changedUpdate(DocumentEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static void playGamelopp() {
refreshGUI();
for (int i =0; i < 15; i++) {
run = true;
while(run) {
card.show(content, Integer.toString(i));
if(run) {
System.out.println("void");
}
else {
break;
}
}
}
}
private static void refreshGUI() {
GameGUI.add(content);
GameGUI.repaint();
GameGUI.revalidate();
}
public static void setRun(boolean setVal) {
run = setVal;
}
}
这里有一个带有标签和2个按钮的JPanel表单
public class QuizzF extends JPanel {
private static String Question;
private String OptionA;
private String OptionB;
private String OptionC;
private String OptionD;
private static String Correct;
public QuizzF(String Question, String OptionA, String OptionB, String OptionC, String OptionD, String CorrectOption) {
initComponents();
HashMap <String, JButton> buttons = new HashMap<String, JButton>();
this.setQuestion(Question);
this.setOptionA(OptionA);
this.setOptionB(OptionB);
this.setOptionC(OptionC);
this.setOptionD(OptionD);
this.setCorrect(CorrectOption);
jLabel1.setText(Question);
A.setText(OptionA);
B.setText(OptionB);
C.setText(OptionC);
D.setText(OptionD);
buttons.put("A", A);
buttons.put("B", B);
buttons.put("C", C);
buttons.put("D", D);
customizeButtons(buttons);
}
private static void customizeButtons(HashMap <String, JButton> buttons) {
for(HashMap.Entry<String, JButton> entry: buttons.entrySet()) {
String name = entry.getKey();
JButton b = entry.getValue();
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("Question = "+getQuestion()+"");
System.out.println("Name = "+name+"");
System.out.println("Correct Answer = "+getCorrect()+"");
if(name.equals(getCorrect())){
b.setBackground(Color.GREEN);
//Module.incrementScore();
}
else {
b.setBackground(Color.RED);
}
Module.setRun(false);
}
});
}
}
public static String getQuestion() {
return Question;
}
public void setQuestion(String question) {
Question = question;
}
public String getOptionA() {
return OptionA;
}
public void setOptionA(String optionA) {
OptionA = optionA;
}
public String getOptionB() {
return OptionB;
}
public void setOptionB(String optionB) {
OptionB = optionB;
}
public String getOptionC() {
return OptionC;
}
public void setOptionC(String optionC) {
OptionC = optionC;
}
public String getOptionD() {
return OptionD;
}
public void setOptionD(String optionD) {
OptionD = optionD;
}
public static String getCorrect() {
return Correct;
}
public void setCorrect(String correct) {
Correct = correct;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
A = new javax.swing.JButton();
B = new javax.swing.JButton();
C = new javax.swing.JButton();
D = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setMinimumSize(new java.awt.Dimension(600, 400));
setName(""); // NOI18N
A.setBackground(new java.awt.Color(30, 37, 81));
A.setForeground(new java.awt.Color(255, 255, 255));
A.setText("Reponse 1");
A.setToolTipText("");
A.setBorder(new javax.swing.border.MatteBorder(null));
A.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
B.setBackground(new java.awt.Color(30, 37, 81));
B.setForeground(new java.awt.Color(255, 255, 255));
B.setText("Reponse 2");
B.setToolTipText("");
B.setBorder(new javax.swing.border.MatteBorder(null));
C.setBackground(new java.awt.Color(30, 37, 81));
C.setForeground(new java.awt.Color(255, 255, 255));
C.setText("Reponse 3");
C.setToolTipText("");
C.setBorder(new javax.swing.border.MatteBorder(null));
D.setBackground(new java.awt.Color(30, 37, 81));
D.setForeground(new java.awt.Color(255, 255, 255));
D.setText("Reponse 4");
D.setToolTipText("");
D.setBorder(new javax.swing.border.MatteBorder(null));
jLabel1.setText("Internet tendent à reproduire le même extrait sans fin, ce qui fait de lipsum.com le seul vrai générateur de Lorem Ipsum.");
jLabel1.setToolTipText("");
jLabel1.setAutoscrolls(true);
jLabel2.setIcon(new javax.swing.ImageIcon("home3.jpg")); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(190, 190, 190)
.addComponent(A, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(190, 190, 190)
.addComponent(B, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(190, 190, 190)
.addComponent(C, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(190, 190, 190)
.addComponent(D, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 560, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(120, 120, 120)
.addComponent(A, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addComponent(B, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(220, 220, 220)
.addComponent(C, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(270, 270, 270)
.addComponent(D, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2)
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JButton A;
private javax.swing.JButton B;
private javax.swing.JButton C;
private javax.swing.JButton D;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
最后,问题与线程有关,我创建了一个新线程,并调用main
类,如下所示:
new SwingWorker() {
@Override
protected Object doInBackground() throws Exeption {
return new Main();
}
}.execute();
问题内容: 我想做的事: 我想列出数据库的一些记录。此列表应显示在 JFrame弹出窗口中。 描述: 我有3节课: Main.java(运行程序) PeopleTableModel.java(保存数据,扩展AbstractTableModel) PeopleTable.java(保存逻辑,扩展JTable) 将JFrame设置为 可见时,为什么会出现ArrayIndexOutOfBoundsExc
我知道同样的问题已经被问过很多次了,但是我似乎真的没有在我的代码中发现阻碍JPanel类型的对象显示在JFrame中的错误。下面是扩展JFrame的类的构造函数: 当我运行main方法(这里没有显示)时,它只显示框架和按钮。如果有人能在这方面给点提示,我会非常感谢的。
因此,我试图在NetBeans IDE中创建swing gui。我对java和GUI的构建是新手,所以这是一个学习曲线。 我创建了一个名为“open”的JMenuItem,并使用JFileChooser打开并显示一个文件。我限制自己使用的文件扩展名是“*.map”。 所以我让我的代码工作了,它在我的桌面上的一个新窗口中打开了选定的文件。但我想知道如何打开JFrame中的文件,而不是一个新窗口。它不
问题内容: 我想在同一JFrame中显示同一图像的变体,例如在JFrame中显示图像,然后将其替换为同一图像的灰度。 问题答案: 每当您更新图像时,都必须重新粉刷。 这是一个有关该主题的简单Google提出的内容:(我将这些教程用于我的所有Java编码) Java教程:绘制图像
问题内容: 我有点卡住。为什么不做这项工作?我只是收到一条错误消息: java.lang.NoSuchMethodError:主要 线程“主”中的异常 问题答案: main需要是静态的,并且必须具有String []而不是String的参数。 为了解决这个问题,将所有东西都放在构造函数中,例如
问题内容: 我是的新手。在我的应用程序中,我想在或中显示。 谁能帮我吗? 问题答案: