我正在尝试将一些监听器放在我的代码中,但我不能。例如,我想添加一个监听器,当我在文本区域中编写somenthing时,我选择JRadioButton“From TextField”,之后我按下“Do It”按钮,我想看到我写的文本(在JTextArea上的TextField)。怎么可能?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javafx.scene.Group;
public class Layout {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
GridBagConstraints c1 = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;}
JTextField text = new JTextField( "Some Text");
if (shouldWeightX) {
c.weightx = 0.5;}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.ipady = 50;
pane.add(text, c);
String names[] = { "Ferrari", "Koenigsegg", "Alfa Romeo" };
JComboBox cb = new JComboBox( names );
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
c.ipady = 50;
pane.add(cb, c);
Box box=Box.createVerticalBox();
JCheckBox checkbox = new JCheckBox("Water");
JCheckBox checkbox2 = new JCheckBox("Fire");
JCheckBox checkbox3 = new JCheckBox("Earth");
c1.fill = GridBagConstraints.HORIZONTAL;
c1.weightx = 0.5;
c1.ipady = 5;
box.add(checkbox);
box.add(checkbox2);
box.add(checkbox3);
pane.add(box,c1);
JPanel container = new JPanel();
container.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
container.setLayout(new GridLayout(2, 1));
JSlider slider = new JSlider(JSlider.HORIZONTAL,0,100,50);
slider.setPreferredSize(new Dimension(150,20));
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(slider, c);
JTextField text1 = new JTextField(" ");
c.ipady = 80;
c.weighty = 2.0;
c.anchor = GridBagConstraints.BASELINE;
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 2;
pane.add(text1, c);
JRadioButton b1= new JRadioButton("From JTextField");
JRadioButton b2= new JRadioButton("From JComboBox");
JRadioButton b3= new JRadioButton("From JCheckboxes");
JRadioButton b4= new JRadioButton("J Slider");
b1.setForeground(Color.BLUE);
b2.setForeground(Color.RED);
b3.setBackground(Color.GREEN);
Box box1 = Box.createHorizontalBox();
box1.add(b1);
box1.add(b2);
box1.add(b3);
box1.add(b4);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 10;
c.weighty = 1.0;
c.anchor = GridBagConstraints.BASELINE;
c.gridx = 0;
c.gridwidth = 3;
c.gridy =2;
pane.add(box1,c);
button = new JButton("Do it");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady =0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.insets = new Insets(10,0,0,0);
c.gridx = 0;
c.gridwidth =3;
c.gridy = 2;
pane.add(button, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame(" ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
frame.setSize(600,400);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
答案 0 :(得分:1)
阅读How to Use Text Areas上的Swing教程中的部分。第一个示例显示如何将文本从JTextField附加到JTextArea。此示例使用ActionListener作为文本字段。
然后我选择JRadioButton“From TextField”,之后我按下“Do It”按钮,我想看到我在JTextArea上写的文本(在TextField中)。
除了将ActionListener添加到按钮之外,概念将类似。然后在调用它时,检查是否选中了“From TextField”单选按钮。如果是这样,那么您将从文本字段中获取文本并将其附加到文本区域。