Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java GUI中左右移动文本_Java_Swing_Animation - Fatal编程技术网

在Java GUI中左右移动文本

在Java GUI中左右移动文本,java,swing,animation,Java,Swing,Animation,我应该制作一个程序,如果你点击顶部不同的按钮,背景颜色会改变,如果你点击底部的左键和右键,它会移动文本。我已经让前一部分工作,但当我点击他们的按钮时,我似乎无法让文本左右移动 我该如何解决这个问题 import javafx.application.Application; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent;

我应该制作一个程序,如果你点击顶部不同的按钮,背景颜色会改变,如果你点击底部的左键和右键,它会移动文本。我已经让前一部分工作,但当我点击他们的按钮时,我似乎无法让文本左右移动

我该如何解决这个问题

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);

    }

}

首先,这不是一个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);
        }
    }
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.ComponentOrientation;
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.KeyListener;
导入javax.swing.BorderFactory;
导入javax.swing.ButtonGroup;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JRadioButton;
导入javax.swing.JScrollPane;
导入javax.swing.JTextField;
导入javax.swing.ScrollPaneConstants;
公共类Unit08_Prog1扩展JFrame实现ActionListener{
//添加单选按钮(颜色)
ButtonGroup颜色=新建ButtonGroup();
JRadioButton red=新的JRadioButton(“红色”);
JRadioButton yellow=新的JRadioButton(“黄色”);
JRadioButton白色=新的JRadioButton(“白色”);
JRadioButton橙色=新JRadioButton(“橙色”);
JRadioButton绿色=新JRadioButton(“绿色”);
//添加文本区域
JTextField textArea=新的JTextField(“欢迎使用Java”);
JScrollPane滚动条;
//添加按钮
JButton btLeft=新JButton(“左”);
JButton btRight=新JButton(“右”);
键盘监听器;
/**
*@Description:使用3个面板填充框架
*
*第一次更改颜色第二次编辑文本第三次包含两个按钮:
*清除并退出具有“C”和“Q”助记符的
*/
公开考试(){
setLayout(新的BorderLayout());
//填充按钮组
颜色。添加(红色);
颜色。添加(黄色);
颜色。添加(白色);
颜色。添加(橙色);
颜色。添加(绿色);
//创建面板1(面板更改文本区域的颜色)
JPanel colorPanel=新的JPanel();
setLayout(新的FlowLayout());
colorPanel.setOrder(BorderFactory.createTitledBorder(“选择消息背景”);
彩色面板。添加(红色);
彩色面板。添加(黄色);
彩色面板。添加(白色);
彩色面板。添加(橙色);
彩色面板。添加(绿色);
//创建面板2(将文本区域添加到框架)
JPanel textPanel=新的JPanel();
setLayout(新的BorderLayout());
textPanel.setboorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
滚动=新的JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL\u SCROLLBAR\u ALWAYS);
textPanel.add(滚动,BorderLayout.CENTER);
//创建面板3(将按钮添加到框架)
JPanel btnPanel=新的JPanel();
btnPanel.add(btLeft);
btnPanel.add(btRight);
//添加面板
添加(颜色面板,边框布局。北);
添加(textPanel,BorderLayout.CENTER);
添加(btnPanel,BorderLayout.SOUTH);
//添加侦听器
red.addActionListener(这个);
黄色。addActionListener(此);
orange.addActionListener(这个);
white.addActionListener(此);
green.addActionListener(this);
btLeft.addActionListener(这个);
btRight.addActionListener(这个);
btLeft.set助记符('c');
btRight.setmemmonic('q');
//addKeyListener((KeyListener)this);
}
/**
*从文本字段处理键类型的事件。
*/
//public void keyTyped(KeyEvent e){
//int key=e.getKeyCode();
//if(key==KeyEvent.VK_C){
//setText(“欢迎使用Java”);
//}else if(key==KeyEvent.VK_Q){
//系统出口(0);
//      }
//    }
//     
///**处理文本字段中的按键事件*/
//按下公共无效键(按键事件e){
//int key=e.getKeyCode();
//if(key==KeyEvent.VK_C){
//setText(“欢迎使用Java”);
//}else if(key==KeyEvent.VK_Q){
//系统出口(0);
//      }
//    }
//     
///**处理文本字段中的密钥释放事件*/
//公共无效密钥已释放(密钥事件e){
//int key=e.getKeyCode();
//      
@凌驾
已执行的公共无效操作(操作事件arg0){
如果(arg0.getSource()==红色){
textArea.setBackground(颜色:红色);
}else if(arg0.getSource()=黄色){
文本区域.立根背景(颜色.黄色);
}else if(arg0.getSource()==橙色){
文本区域.背景(颜色.浅灰色);
}else if(arg0.getSource()=白色){
textArea.setBackground(颜色:白色);
}else if(arg0.getSource()=绿色){
textArea.setBackground(颜色为绿色);
}else if(“Left”.equals(arg0.getActionCommand())){
textArea.applycomponentationation(组件方向从左到右);
}else if(“Right”.equals(arg0.getActionCommand())){
textArea.applycomponentation(组件方向。从右到左);
}
}
}

再一次,我没有对您的代码进行任何更改,但我认为您应该进行更改。

您可以在
标签上使用相同的想法。
。此外,为了避免您以后的头痛,请删除
awt
导入