我使用Nimbus LAF,我想改变一个简单的背景JButton.
JButton jbutton = new JButton("test");
jbutton.setBackground(Color.BLACK);
Run Code Online (Sandbox Code Playgroud)
但它不起作用,当我改变外观并感觉它有效但它在Nimbus中不起作用.
我该怎么做?
谢谢你的帮助.
也许我会以错误的方式解决这个问题.让我知道使用Swing和AWT,我在一个框架上设置了几个按钮,它们每个都有一个ActionListener对应于它们的特定功能IE
JButton foo_button = new JButton("Foo-Me");
foo_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//Fancy schmancy code work
}
})
JButton bar_button = new JButton("Bar None");
bar_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//Fancy schmancy code work
}
})
Run Code Online (Sandbox Code Playgroud)
所以每个按钮都做自己的事情.但是,如果我希望所有按钮都做某件事(每个按钮的方法相同),在我的情况下,清除标签,然后再做他们自己的事情.
显然我可以将whatever_label.setText("")添加到每个actionPerformed(),但这需要大量的重复,我不是那么喜欢的东西.
哦Java和Swing大师来帮助我.
我有两个JPanel.在第一个中我有3个JButtons,第二个是根据从键盘读取的事件绘制图像.如果我设置了JButtons,setEnabled(false);我可以像我期望的那样使用键盘事件(如果我向上按箭头,图像会向上移动),但是当按钮启用时,图像没有任何反应.甚至,如果我按下空格键,它就像我点击按钮一样.
所以我有一个JList,我试图在一个内部使用它JButton小号actionPerformed方法,它要求我做出JList final这是为什么,下面的代码片段
public SomeClass() {
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
list.clearSelection();
}});
}
Run Code Online (Sandbox Code Playgroud)
我实际上没有问题使它成为最终的,我只是不确定为什么我需要.
我有一个类Game和一组按钮buts,我想fun(i,j)在点击任何buts [i] [j]时调用Game类的函数,我试着这样:
buts = new JButton[Setting.length][Setting.width];
Game game = new Game(setting);
int hgap = 4, vgap = 4;
panel = new JPanel(new GridLayout(Setting.length, Setting.width, hgap, vgap));
for (int i = 0; i < Setting.length; i++) {
for (int j = 0; j < Setting.width; j++) {
//JButton btn = new JButton();
buts[i][j] = new JButton();
buts[i][j].setText(String.valueOf(j));
setColor(buts[i][j], Game.cells[i][j].getColor());
buts[i][j].addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
panel.add(buts[i][j]);
}
} …Run Code Online (Sandbox Code Playgroud) 你好,我有这个设置
private JButton btnFoo, btnBar;
Run Code Online (Sandbox Code Playgroud)
我需要为每个按钮获取以下内容
btnFoo = new JButton("Foo");
btnFoo.addActionListener(this);
add(btnFoo);
Run Code Online (Sandbox Code Playgroud)
在Java中是否可以为我声明的每个按钮动态创建它?因为当我有5个按钮时,我不想要3x5 = 15行代码,而只需要几行动态创建按钮.
我在JButton上设置ActionListener时遇到了一些麻烦,这里是代码......
package pipes;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PipesUI extends javax.swing.JFrame {
Main main = new Main();
JButton addPipeButton = new JButton("Add Pipe");
public PipesUI(){
addUI();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addUI(){
addPipeButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==addPipeButton)
main.addPipe();
else
;
}
public static void main(String args[]) {
PipesUI pipesUI = new PipesUI(); // create an instance of the menu
pipesUI.setSize(500,500);
pipesUI.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
错误发生在addPipeButton.addActionListener(this)行;
(这)它似乎不喜欢,错误说'不兼容的类型:PipesUI无法转换为ActionListener'
任何帮助都会非常感谢.
我使用下面的代码,但没有正常工作.我JButton在面板上有对象列表但无法单独单击每个按钮.
for(int i=0; i<udataArr.length(); i++) {
userBtn = new JButton();
userLb = new JLabel();
cur1 = userBtn.getCursor();
userBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
Image imgUO = ImageIO.read(getClass().getResource("/resources/img-std.png"));
userBtn.setIcon(new ImageIcon(imgUO));
userBtn.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
userLb.setText((String) udataArr.getJSONObject(i).get("user_name"));
//button[i].setText((String) udataArr.getJSONObject(i).get("user_name"));
panelLeft.add(userBtn);
panelLeft.add(userLb);
panelLeft.add(Box.createVerticalStrut(15));
}
Run Code Online (Sandbox Code Playgroud)

我有一个项目要求我创建一个简单的GUI,顶部有一个jTextArea,JButtons从0到9添加.这很简单,但程序要求我将0置于底部.下面的代码完成了,但是我必须使用Grid Layout而不是flowLayout.当我用GridLayout编写它时,除了左侧,我无法将0对齐到任何东西.如何使用GridLayout,并将0居中?
public class CalculatorProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GUI gui = new GUI();
}
}
public class GUI extends JFrame {
public GUI() {
JFrame aWindow = new JFrame("Calculator");
aWindow.setBounds(30, 30, 175, 215); // Size
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField jta = new JTextField(20);
FlowLayout flow = new FlowLayout(); // Create a layout manager
flow.setHgap(5); // Set the horizontal gap
flow.setVgap(5);
flow.setAlignment(FlowLayout.CENTER);
Container content = aWindow.getContentPane(); // Get the content …Run Code Online (Sandbox Code Playgroud) 我被问到:
JFrame)我已经尝试过了,但是我不知道怎么做。我唯一成功完成的事情就是将两个按钮放在同一位置BorderLayout(例如,两个按钮位于“中心”位置,但我认为这与“一个按钮中的一个按钮”不是同一件事)。
如果有人知道是否有可能做到或如何做到,那就太好了!