设置JLabel在Jradiobuttons上可见单击

时间:2018-11-10 14:36:31

标签: eclipse jlabel

我尝试将JLabel设置为仅在选择某个RadioButton之后才可见。如果我在单选按钮执行的操作中设置了lblNewLabel_1.setVisible(true),则程序会出错。它与textField一起工作,但是没有。我能做什么? JLabel是否有所不同?有人可以给我一些建议吗?

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.ButtonGroup;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Fereastra extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Fereastra frame = new Fereastra();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Fereastra() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 751, 565);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(12, 13, 447, 251);
		contentPane.add(textArea);
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("Cautarea pe nivel");
		rdbtnNewRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnNewRadioButton.isSelected())
				{
					textField.setVisible(false);
					textField_1.setVisible(false);
	


				}
				
			}
		});
		buttonGroup.add(rdbtnNewRadioButton);
		rdbtnNewRadioButton.setBounds(488, 55, 185, 25);
		contentPane.add(rdbtnNewRadioButton);		
		
		JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Cautarea in adancime");
		rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(rdbtnNewRadioButton_1.isSelected())
				{
					textField.setVisible(true);
					textField_1.setVisible(true);

				}
				else
				{
					textField.setVisible(false);
					textField_1.setVisible(false);
			


				}
				
			}
		});
		buttonGroup.add(rdbtnNewRadioButton_1);
		rdbtnNewRadioButton_1.setBounds(488, 96, 237, 25);
		contentPane.add(rdbtnNewRadioButton_1);
					
		JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("Costul uniform");
		rdbtnNewRadioButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnNewRadioButton_2.isSelected())
				{
					textField.setVisible(false);
					textField_1.setVisible(false);

				}
			}
		});
		buttonGroup.add(rdbtnNewRadioButton_2);
		rdbtnNewRadioButton_2.setBounds(488, 138, 127, 25);
		contentPane.add(rdbtnNewRadioButton_2);
		
		JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("Adancime iterativa");
		rdbtnNewRadioButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnNewRadioButton_3.isSelected())
				{
					textField_1.setVisible(true);
					textField.setVisible(true);
				}
				else
				{
					textField_1.setVisible(false);
					textField.setVisible(false);


				}
			}
		});
		buttonGroup.add(rdbtnNewRadioButton_3);
		rdbtnNewRadioButton_3.setBounds(488, 179, 237, 25);
		contentPane.add(rdbtnNewRadioButton_3);
		
		JRadioButton rdbtnNewRadioButton_4 = new JRadioButton("Greedy");
		rdbtnNewRadioButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnNewRadioButton_4.isSelected())
				{
					textField.setVisible(false);
					textField_1.setVisible(false);


				}
			}
		});
		buttonGroup.add(rdbtnNewRadioButton_4);
		rdbtnNewRadioButton_4.setBounds(488, 221, 127, 25);
		contentPane.add(rdbtnNewRadioButton_4);
		
		JLabel lblNewLabel = new JLabel("Alegeti:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNewLabel.setBounds(488, 13, 84, 33);
		contentPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(402, 277, 116, 22);
		contentPane.add(textField);
		textField.setColumns(10);
		textField.setVisible(false);
		
		textField_1 = new JTextField();
		textField_1.setBounds(246, 277, 116, 22);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		textField_1.setVisible(false);

		
		JLabel lblNewLabel_1 = new JLabel("Introduceti valorile dorite:");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNewLabel_1.setBounds(22, 277, 212, 21);
		contentPane.add(lblNewLabel_1);
		lblNewLabel_1.setVisible(false);
		
	}
}

1 个答案:

答案 0 :(得分:0)

这是因为您的标签是在函数上下文中声明的,所以将声明放在文本框声明的旁边,它应该可以正常工作