我怎样才能做到这一点?
我目前的代码是:
import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.border.Border;
import javax.swing.*;
public class Bengali extends JFrame {
private JMenuBar menuBar;
private JButton[][] buttons;
private int i,j;
private String hex;
//Constructor
public Bengali(){
this.setTitle("GUI_project");
this.setSize(1600,140);
//menu generate method
generateMenu();
this.setJMenuBar(menuBar);
//pane with null layout
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(1600,140));
contentPane.setBackground(new Color(192,192,192));
buttons=new JButton[4][32];
for(i=0;i<4;i++)
{
for(j=0;j<32;j++)
{
buttons[i][j]=new JButton();
buttons[i][j].setBounds(0+50*j,0+35*i,50,35);
buttons[i][j].setBackground(new Color(214,217,223));
buttons[i][j].setForeground(new Color(0,0,0));
buttons[i][j].setEnabled(true);
buttons[i][j].setFont(new Font("sansserif",0,12));
hex = "u"+Integer.toHexString(i+2432);
buttons[i][j].setText(hex);
buttons[i][j].setVisible(true);
//Set methods for mouse events
//Call defined methods
buttons[i][j].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
but(evt,i,j);
}
});
//adding components to contentPane panel
contentPane.add(buttons[i][j]);
}
//adding panel to JFrame and seting of window position and close operation
this.add(contentPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
}
}
//Method mouseClicked for buttons[i][j]
private void but (MouseEvent evt,int i,int j)
{
//TODO
}
//method for generate menu
public void generateMenu()
{
menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu tools = new JMenu("Tools");
JMenu help = new JMenu("Help");
JMenuItem open = new JMenuItem("Open ");
JMenuItem save = new JMenuItem("Save ");
JMenuItem exit = new JMenuItem("Exit ");
JMenuItem preferences = new JMenuItem("Preferences ");
JMenuItem about = new JMenuItem("About ");
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
tools.add(preferences);
help.add(about);
menuBar.add(file);
menuBar.add(tools);
menuBar.add(help);
}
public static void build(){
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Bengali();
}
});
}
}
GUI已经准备好了。使用SimpleJUI扩展为BlueJ制作。
答案 0 :(得分:0)
使用Robot
类可以很容易地完成此操作。您必须模拟按键(而不是“写入任何输入流”,因为这不起作用)。
这篇文章接受的答案很好地描述了这一点: Press a key with Java
答案 1 :(得分:0)
您可以使用PipedInputStream
和PipedOutputStream
课程。
每当发生事件时,您必须写入输出流,并且您应该有一个输入流的侦听器。您可能已经在两个不同的线程中运行它们。
示例摘录:
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
如果你不想丢失任何数据,你应该首先启动监听器线程。