我应该制作一个程序,如果你单击顶部不同的按钮,背景颜色会改变,如果你单击左下角和右下按钮,它会移动文本。我已经让前一部分工作了,但是当我点击它们的按钮时,我似乎无法左右移动文本。
我该如何解决这个问题?
import javafx.application.Application;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static java.awt.FlowLayout.LEFT;
import static javax.swing.border.TitledBorder.LEFT;
import static javax.swing.JSplitPane.LEFT;
import static javax.swing.SwingConstants.LEFT;
import static java.awt.Event.LEFT;
import static java.awt.Label.LEFT;
import static javafx.geometry.HPos.LEFT;
import static javafx.geometry.HorizontalDirection.LEFT;
import static javafx.geometry.Side.LEFT;
import static javafx.scene.control.ButtonBar.ButtonData.LEFT;
import static javafx.scene.control.ContentDisplay.LEFT;
import static javafx.scene.input.KeyCode.LEFT;
import static javafx.scene.text.TextAlignment.LEFT;
import static com.sun.javafx.scene.traversal.Direction.LEFT;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class Unit08_Prog1 extends JFrame implements ActionListener {
//add Radio Button (colors)
ButtonGroup colors = new ButtonGroup();
JRadioButton red = new JRadioButton("Red");
JRadioButton yellow = new JRadioButton("Yellow");
JRadioButton white = new JRadioButton("White");
JRadioButton orange = new JRadioButton("Orange");
JRadioButton green = new JRadioButton("Green");
//add text area
JTextArea textArea = new JTextArea("Welcome to Java" , 5 , 30);
//add buttons
JButton btLeft = new JButton("Left");
JButton btRight = new JButton("Right");
KeyListener keyListener;
/**
* @Description: populate frame with 3 panels
*
* 1st changes colors 2nd edits text 3rd contains two buttons:
* Clear and Quit which have Mnemonics of "C" and "Q"
*/
public Unit08_Prog1() {
setLayout(new BorderLayout());
//populate ButtonGroup
colors.add(red);
colors.add(yellow);
colors.add(white);
colors.add(orange);
colors.add(green);
// create panel 1 (panel changes text area's color)
JPanel colorPanel = new JPanel();
colorPanel.setLayout(new FlowLayout());
colorPanel.setBorder(BorderFactory.createTitledBorder("Select Message Background"));
colorPanel.add(red);
colorPanel.add(yellow);
colorPanel.add(white);
colorPanel.add(orange);
colorPanel.add(green);
//Create panel 2 (add text area to frame)
JPanel textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
textPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
JScrollPane scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textPanel.add(scroll, BorderLayout.CENTER);
//create panel 3 (add buttons to frame)
JPanel btnPanel = new JPanel();
btnPanel.add(btLeft);
btnPanel.add(btRight);
//add panels
add(colorPanel, BorderLayout.NORTH);
add(textPanel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
//add listeners
red.addActionListener(this);
yellow.addActionListener(this);
orange.addActionListener(this);
white.addActionListener(this);
green.addActionListener(this);
btLeft.addActionListener(this);
btRight.addActionListener(this);
btLeft.setMnemonic('c');
btRight.setMnemonic('q');
// addKeyListener((KeyListener) this);
}
/** Handle the key typed event from the text field. */
// public void keyTyped(KeyEvent e) {
// int key = e.getKeyCode();
// if (key == KeyEvent.VK_C){
// textArea.setText("Welcome to Java");
// }else if(key == KeyEvent.VK_Q){
// System.exit(0);
// }
// }
//
// /** Handle the key pressed event from the text field. */
// public void keyPressed(KeyEvent e) {
// int key = e.getKeyCode();
// if (key == KeyEvent.VK_C){
// textArea.setText("Welcome to Java");
// }else if(key == KeyEvent.VK_Q){
// System.exit(0);
// }
// }
//
// /** Handle the key released event from the text field. */
// public void keyReleased(KeyEvent e) {
// int key = e.getKeyCode();
//
public static void main(String[] args) {
// create frame
Unit08_Prog1 frame = new Unit08_Prog1();
frame.pack();
frame.setMinimumSize(new Dimension(400,200));
frame.setTitle("Unit08_Prog1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() == red){
textArea.setBackground(Color.RED);
}else if(arg0.getSource() == yellow){
textArea.setBackground(Color.YELLOW);
}else if(arg0.getSource() == orange){
textArea.setBackground(Color.LIGHT_GRAY);
}else if(arg0.getSource() == white){
textArea.setBackground(Color.WHITE);
}else if(arg0.getSource() == green){
textArea.setBackground(Color.GREEN);
}
}
答案 0 :(得分:0)
首先,这不是JavaFX应用程序。您的代码中没有一个元素会使用FX平台。
其次,我认为你应该考虑你的代码。在我看来,实施ActionListener
并在一个地方处理不同类型的行动并不是一个好主意。
所以,我发现使用JTextArea
对齐文本有点问题,因为虽然它确实有效,但当你更改区域内的文本时它会开始工作,这很奇怪。
您可以使用JTextField
吗?如果可以,这里将是您特定案例的解决方案:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class Unit08_Prog1 extends JFrame implements ActionListener {
//add Radio Button (colors)
ButtonGroup colors = new ButtonGroup();
JRadioButton red = new JRadioButton("Red");
JRadioButton yellow = new JRadioButton("Yellow");
JRadioButton white = new JRadioButton("White");
JRadioButton orange = new JRadioButton("Orange");
JRadioButton green = new JRadioButton("Green");
//add text area
JTextField textArea = new JTextField("Welcome to Java");
JScrollPane scroll;
//add buttons
JButton btLeft = new JButton("Left");
JButton btRight = new JButton("Right");
KeyListener keyListener;
/**
* @Description: populate frame with 3 panels
* <p>
* 1st changes colors 2nd edits text 3rd contains two buttons:
* Clear and Quit which have Mnemonics of "C" and "Q"
*/
public Test() {
setLayout(new BorderLayout());
//populate ButtonGroup
colors.add(red);
colors.add(yellow);
colors.add(white);
colors.add(orange);
colors.add(green);
// create panel 1 (panel changes text area's color)
JPanel colorPanel = new JPanel();
colorPanel.setLayout(new FlowLayout());
colorPanel.setBorder(BorderFactory.createTitledBorder("Select Message Background"));
colorPanel.add(red);
colorPanel.add(yellow);
colorPanel.add(white);
colorPanel.add(orange);
colorPanel.add(green);
//Create panel 2 (add text area to frame)
JPanel textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
textPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textPanel.add(scroll, BorderLayout.CENTER);
//create panel 3 (add buttons to frame)
JPanel btnPanel = new JPanel();
btnPanel.add(btLeft);
btnPanel.add(btRight);
//add panels
add(colorPanel, BorderLayout.NORTH);
add(textPanel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
//add listeners
red.addActionListener(this);
yellow.addActionListener(this);
orange.addActionListener(this);
white.addActionListener(this);
green.addActionListener(this);
btLeft.addActionListener(this);
btRight.addActionListener(this);
btLeft.setMnemonic('c');
btRight.setMnemonic('q');
// addKeyListener((KeyListener) this);
}
/**
* Handle the key typed event from the text field.
*/
// public void keyTyped(KeyEvent e) {
// int key = e.getKeyCode();
// if (key == KeyEvent.VK_C){
// textArea.setText("Welcome to Java");
// }else if(key == KeyEvent.VK_Q){
// System.exit(0);
// }
// }
//
// /** Handle the key pressed event from the text field. */
// public void keyPressed(KeyEvent e) {
// int key = e.getKeyCode();
// if (key == KeyEvent.VK_C){
// textArea.setText("Welcome to Java");
// }else if(key == KeyEvent.VK_Q){
// System.exit(0);
// }
// }
//
// /** Handle the key released event from the text field. */
// public void keyReleased(KeyEvent e) {
// int key = e.getKeyCode();
//
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == red) {
textArea.setBackground(Color.RED);
} else if (arg0.getSource() == yellow) {
textArea.setBackground(Color.YELLOW);
} else if (arg0.getSource() == orange) {
textArea.setBackground(Color.LIGHT_GRAY);
} else if (arg0.getSource() == white) {
textArea.setBackground(Color.WHITE);
} else if (arg0.getSource() == green) {
textArea.setBackground(Color.GREEN);
} else if ("Left".equals(arg0.getActionCommand())) {
textArea.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
} else if ("Right".equals(arg0.getActionCommand())) {
textArea.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
}
}
再一次,我没有对您的代码进行任何更改,但我认为您应该这样做。