当这个程序运行时,它默认显示默认输出,当我点击活动按钮时,没有任何显示。请帮我解决这个问题。
我想开发一个可以像这个应用程序一样工作的应用程序。这是一个 演示应用程序,但我无法解决此问题。
演示应用程序:
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
public class GraphicRadioButton extends JFrame{
public static JRadioButton active ,deactive;
private ButtonGroup rdoBtn;
private JFrame frame;
private JPanel panel;
public GraphicRadioButton()
{
super("Testing");
active = new JRadioButton("Active");
deactive = new JRadioButton("Deactive ", true);
frame = new JFrame();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new FlowLayout());
rdoBtn = new ButtonGroup();
rdoBtn.add(active);
rdoBtn.add(deactive);
panel.add(active);
panel.add(deactive);
this.add(panel,BorderLayout.CENTER);
this.setResizable(false);
Cursor c = new Cursor(Cursor.HAND_CURSOR);
this.setCursor(c);
pack();
}
}
import javax.swing.*;
public class Main {
public static void main(String ar[]){
GraphicRadioButton g = new GraphicRadioButton();
g.setVisible(true);
boolean test = true;
boolean ch = true;
do{
if(GraphicRadioButton.active.isSelected()&&!test){
System.out.println("active");
test = true;
ch = true;
}
if(GraphicRadioButton.deactive.isSelected() &&ch ){
System.out.println("deactive");
test = false;
ch = false;
}
}while(true);
}//end main method
}//end Main class
答案 0 :(得分:0)
更改此行
if (GraphicRadioButton.active.isSelected() && !test) {
到
if (GraphicRadioButton.active.isSelected() && test) {
您需要将!test
更改为test
。
答案 1 :(得分:0)
我已使用以下代码进行测试,并且工作正常:
package ljm.aplication;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class GraphicRadioButton extends JFrame {
public static void main(String ar[]) {
GraphicRadioButton g = new GraphicRadioButton();
g.setVisible(true);
boolean test = true;
boolean ch = true;
do {
if (GraphicRadioButton.active.isSelected() && !test) {
System.out.println("active");
test = true;
ch = true;
}
if (GraphicRadioButton.deactive.isSelected() && ch) {
System.out.println("deactive");
test = false;
ch = false;
}
}
while (true);
}// end main method
private static final long serialVersionUID = -3366748875509458022L;
public static JRadioButton active, deactive;
private ButtonGroup rdoBtn;
private JFrame frame;
private JPanel panel;
public GraphicRadioButton() {
super("Testing");
active = new JRadioButton("Active");
deactive = new JRadioButton("Deactive ", true);
frame = new JFrame();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new FlowLayout());
rdoBtn = new ButtonGroup();
rdoBtn.add(active);
rdoBtn.add(deactive);
panel.add(active);
panel.add(deactive);
this.add(panel, BorderLayout.CENTER);
this.setResizable(false);
Cursor c = new Cursor(Cursor.HAND_CURSOR);
this.setCursor(c);
pack();
}
}