사용자가 만든 버튼을 누를 때 TextField가으로 편집 가능합니다. 먼저 생성자의 조건을 다음과 같이 사용합니다.
if(button.isSelected()) TextField.isEditable(true); else TextField.isEditable(false);
그러나 이것은 오류입니다. 그런 다음 나는 에 의해 다른 method_implemented 인 매개 변수 ActionEvent
이있는 메서드에서 동일한 문을 사용하여 (사용자가 편집 가능한 텍스트를 만들 수 있도록 사용자 날씨에 대한 권한 부여) 사용합니다. 그러나 이것은 또한 오류를줍니다. 버튼 조치를 적용하기 전에
코드 및 출력이 주어진다 : 코드가 먼저
Output
'JTextFields'에서 사용자가 TextField를 편집 가능하게 만들 수있는 방법이 있습니까? 기본 대소 문자는 편집 할 수 없습니다. 자세히보다.
package Radio_Button;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Radio_Buttons extends JPanel{
private JRadioButton bold;
private JRadioButton italic;
private JRadioButton both;
private JRadioButton plain;
private ButtonGroup group;
private JTextField TextField;
private JButton button;
public Radio_Buttons(){
//Declare all radio buttons
plain = new JRadioButton("Plain",false);
bold = new JRadioButton("Bold", false);
italic = new JRadioButton("Italic", false);
both = new JRadioButton("Bold+Italic", false);
//Declare Text Field
TextField = new JTextField("The quick brown fox jumps over the lazy dog.",40);
TextField.setFont(new Font("Chiller", Font.PLAIN, 30));
//For button
button = new JButton("Push to edit");
//Add in panel
add(bold);
add(italic);
add(both);
add(plain);
add(TextField);
add(button);
//Make a family of radiobuttons so they can understand each others.
group = new ButtonGroup();
group.add(bold);
group.add(italic);
group.add(both);
group.add(plain);
RadioListener listener = new RadioListener();
bold.addActionListener(listener);
italic.addActionListener(listener);
plain.addActionListener(listener);
both.addActionListener(listener);
setBackground(Color.yellow);
setPreferredSize(new Dimension(800,500));
}
private class RadioListener implements ActionListener{
public void actionPerformed(ActionEvent event){
int source = 0;
if (bold.isSelected()) {source =Font.BOLD; }
else if (italic.isSelected()) {source = Font.ITALIC; }
else if (both.isSelected()){source = Font.BOLD+Font.ITALIC; }
else {source = Font.PLAIN; }
TextField.setFont(new Font("Chiller", source, 30));
}
}
public static void main (String [] args){
JFrame frame = new JFrame("Quote Options");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Radio_Buttons panel = new Radio_Buttons();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
은'JButton'하지 않는 아래처럼 Textflied의 편집 변경 이 코드에는'ActionListener'가 있습니다. – Berger