以上就是给各位分享如何在Java中为jbutton添加快捷键?,其中也会对java给按钮添加快捷键进行解释,同时本文还将给你拓展JavaSwing在运行时添加/删除jButton、java–在JBut
以上就是给各位分享如何在Java中为jbutton添加快捷键?,其中也会对java给按钮添加快捷键进行解释,同时本文还将给你拓展Java Swing在运行时添加/删除jButton、java – 在JButton上运行函数、java – 如何在Jbutton的边缘添加间隙?、java – 如何在JScrollPane上添加JButton?等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- 如何在Java中为jbutton添加快捷键?(java给按钮添加快捷键)
- Java Swing在运行时添加/删除jButton
- java – 在JButton上运行函数
- java – 如何在Jbutton的边缘添加间隙?
- java – 如何在JScrollPane上添加JButton?
如何在Java中为jbutton添加快捷键?(java给按钮添加快捷键)
我有一个jButton,我想为其指定一个快捷方式。就像我按键盘上的Delete键一样,它只需单击一次jButton。我怎样才能做到这一点?
答案1
小编典典您需要创建一个Action
供按钮使用的。然后,Action
可以使用,ActionListener
并且可以将绑定Action
到KeyStroke
。
阅读Swing教程。有以下部分:
- 如何使用动作
- 如何使用键绑定
例如:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class CalculatorPanel extends JPanel{ private JTextField display; public CalculatorPanel() { Action numberAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) {// display.setCaretPosition( display.getDocument().getLength() ); display.replaceSelection(e.getActionCommand()); } }; setLayout( new BorderLayout() ); display = new JTextField(); display.setEditable( false ); display.setHorizontalAlignment(JTextField.RIGHT); add(display, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout( new GridLayout(0, 5) ); add(buttonPanel, BorderLayout.CENTER); for (int i = 0; i < 10; i++) { String text = String.valueOf(i); JButton button = new JButton( text ); button.addActionListener( numberAction ); button.setBorder( new LineBorder(Color.BLACK) ); button.setPreferredSize( new Dimension(30, 30) ); buttonPanel.add( button ); InputMap inputMap = buttonPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(KeyStroke.getKeyStroke(text), text); inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text); buttonPanel.getActionMap().put(text, numberAction); } } private static void createAndShowUI() { JFrame frame = new JFrame("Calculator Panel"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.add( new CalculatorPanel() ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); }}
Java Swing在运行时添加/删除jButton
我的应用程序有一个模块,允许用户在运行时在jLayeredpane上添加jButton。我想向此动态添加的内容添加动作侦听器,而且我还必须提供在运行时删除动态添加的按钮的访问权限。有什么办法吗?
private Map<String,JButton> dynamicButtons;
public void addButton(String name) {
JButton b = new JButton(name);
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLayeredPane2.add(b);
dynamicButtons.put(name,b);
jLayeredPane2.invalidate();
}
public void removeButton(String name) {
JButton b = dynamicButtons.remove(name);
jLayeredPane2.remove(b);
jLayeredPane2.invalidate();
}
java – 在JButton上运行函数
我不做很多java工作,这只是一个有趣的事情,我想尝试但是被困住了一段时间.任何帮助表示赞赏.
import java.awt.AWTException; import java.awt.FlowLayout; import java.awt.Robot; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class Main extends JFrame { /** * */ private static final long serialVersionUID = 1L; private static boolean running = false;; private JButton start_button; private JButton stop_button; private JLabel tl; private static int i = 0; Robot robot; void start() { JFrame frame = new JFrame("Helper"); tl = new JLabel("Running: " + running); start_button = new JButton("Start"); stop_button = new JButton("Stop"); stop_button.setEnabled(false); frame.add(tl); frame.add(start_button); frame.add(stop_button); frame.setSize(300,100); frame.setVisible(true); frame.setLayout(new FlowLayout()); frame.setDefaultCloSEOperation(EXIT_ON_CLOSE); frame.setLocation(400,400); try { robot = new Robot(); } catch (AWTException e2) { // Todo Auto-generated catch block e2.printstacktrace(); } robot.setAutoDelay(200); start_button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { start_button.setEnabled(false); stop_button.setEnabled(true); running = true; tl.setText("Running: " + running); while (running) { robot_loop(robot); } } }); stop_button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { start_button.setEnabled(true); stop_button.setEnabled(false); running = false; tl.setText("Running: " + running); } }); } public static void main(String[] args) { new Main().start(); } private static void robot_loop(Robot robot) { robot.keyPress(KeyEvent.VK_NUMPAD0); robot.keyrelease(KeyEvent.VK_NUMPAD0); System.out.println("numpad 0 pressed! - " + i); i++; } }
解决方法
在Swing的事件调度线程上调用这些事件侦听器的actionPerformed方法,并且由于您进入无限循环,它将导致GUI冻结.您可以在actionPerformed方法中创建一个线程,并在新线程内部完成工作.虽然您遇到的下一个问题是找到一种很好的方法来在用户按下停止按钮时停止该线程.
很酷的是,你已经在代码中完成了所有逻辑.让它工作就像改变一样简单:
start_button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { start_button.setEnabled(false); stop_button.setEnabled(true); running = true; tl.setText("Running: " + running); while (running) { robot_loop(robot); } } });
要在自己的线程上完成工作:
start_button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { start_button.setEnabled(false); stop_button.setEnabled(true); running = true; tl.setText("Running: " + running); Executors.newSingleThreadExecutor().submit(new Runnable() { @Override public void run() { while (running) { robot_loop(robot); } } }); } });
上面的代码使用了执行程序框架(java.util.concurrent.*),而不是直接创建一个线程. nachokk建议的另一种方法是使用计时器java.util.Timer或javax.swing.Timer(在这种情况下应该没问题).
java – 如何在Jbutton的边缘添加间隙?
>我最初没有谈到边框,并且得到了我想要的单像素LineBorders,但按钮都相互对接.
>然后我尝试了按钮[i] .setBorder(BorderFactory.createEmptyBorder(5,5,5)).它不是在按钮周围添加空白区域,而是使按钮区域扩展.它还删除了LineBorder.
>然后我尝试了:button [i] .setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,5),button.getBorder()))
这让我回到LineBorder,但不是在线外添加空格,而是将按钮的区域扩展到线外!
我意识到我可以添加空白框来分隔我的按钮,但我也希望它们的两侧都有空间,这就是我想添加一个EmptyBorder的原因.我是Swing的新手,所以也许有一种更好的方法可以做到这一点,我不知道:)
我正在使用Jython,但API应该与Java相同.
解决方法
这实际上是布局问题,因此您应该检查您正在使用的布局管理器的文档.例如:
>某些布局管理器(如FlowLayout,BorderLayout或GridLayout)具有hgap和vgap属性,用于指定组件之间的水平和垂直间隙.
>使用GridBagLayout,您可以设置GridBagConstraints对象的插图.
>使用BoxLayout,您可以添加“刚性区域”,“胶水”和“填充物”(参见Box类).
等等.
java – 如何在JScrollPane上添加JButton?
我的代码如下
public class AddingToJScrollPane { public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Label"); label.setPreferredSize(new Dimension(1000,1000)); JScrollPane jScrollPane = new JScrollPane(label); JButton jButton1 = new JButton("Hello"); JButton jButton2 = new JButton("Hello"); jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane.setViewportBorder(new LineBorder(Color.RED)); jScrollPane.getViewport().add(jButton1,jButton2); frame.add(jScrollPane,BorderLayout.norTH); frame.setSize(400,150); frame.setVisible(true); } }
更新的代码
public class AddingToJScrollPane { public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); JPanel panel = new JPanel(); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); panel.setLayout( new GridLayout() ); JLabel label = new JLabel("Label"); label.setPreferredSize(new Dimension(1000,1000)); JScrollPane jScrollPane = new JScrollPane(panel); JButton jButton1 = new JButton("Hello"); JButton jButton2 = new JButton("He"); // jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane.setViewportBorder(new LineBorder(Color.RED)); // jScrollPane.getViewport().add(panel); frame.add(jScrollPane,BorderLayout.norTH); panel.add(jButton1); panel.add(jButton2); frame.setSize(400,150); frame.setVisible(true); } }
我怎样才能实现我想要的输出
提前致谢
解决方法
关于如何在Java中为jbutton添加快捷键?和java给按钮添加快捷键的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Java Swing在运行时添加/删除jButton、java – 在JButton上运行函数、java – 如何在Jbutton的边缘添加间隙?、java – 如何在JScrollPane上添加JButton?的相关信息,请在本站寻找。
本文标签: