我正在制作一个有许多按钮的摇摆gui.我有许多操作,其中按钮有时禁用和启用.我想为仅启用的按钮设置工具提示.当按钮禁用时,我不想要该按钮的任何工具提示.
我创建了一个文本字段组件,里面有一个"x"按钮,用于清除字段的文本.即,我可以使用不会改变按钮上的利润-这当使用Windows的系统的外观和感觉(Windows版),但我有尝试使用雨云时得到正确的看问题的伟大工程button.setMargin(new Inset(0, 0, 0, 0),我可以用系统的外观感觉
在使用Nimbus时,我确实寻求帮助设置边距,这似乎是一个常见的问题.我能够找到允许我更改边距的部分解决方案,但是它会更改所有按钮的边距,我只希望单个按钮与默认值不同.
我明确地设置'x'按钮大小,根据'x'字符串的边界计算,这样它就足以显示'x'.
使用Window的系统外观时的结果,这是我正在寻找的一般外观:
使用默认Nimbus外观时的结果; 'x'溢出了Nimbus允许的空间,导致省略号.

使用Nimbus并覆盖Button.contentMargins的结果:现在'x'按钮看起来正确,但'test'按钮看起来很糟糕.

源代码:
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LookAndFeel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
public class TextFieldTest {
public static void main(String[] args) {
boolean useNimbusLookAndFeel = false;
boolean overrideNimbusDefaults = false;
String lafName = UIManager.getSystemLookAndFeelClassName();
if ( useNimbusLookAndFeel ) {
for ( LookAndFeelInfo info : UIManager.getInstalledLookAndFeels() ) {
if ( "Nimbus".equals(info.getName()) ) {
lafName = …Run Code Online (Sandbox Code Playgroud) 我遇到了麻烦; 我正在尝试实施影院预订系统但是我无法将actionListener设置为不同网格上的特定按钮.我希望它的工作方式是每个用户都有一个会话,他可以在这个会话中选择座位和票价,如学生费率,老年养老金率.无论如何,我似乎无法添加actionListener,所以当他选择座位时,那些在程序运行时变得不可用.谢谢.
// Load Libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cinemaSystem {
// Global Variables
JFrame frame = new JFrame(); // Creates JFrame
JLabel title;
JButton l[][], m[][], r[][]; // Names grid of JButtons
JPanel panel1, panel2, panel3;
// Constructor
public cinemaSystem(){
title = new JLabel("Cinema Booking");
title.setFont(new Font("Helvetica", Font.BOLD, 30));
title.setLocation(12,5);
title.setSize(600, 60);
frame.setLayout(null); // Setting Grid Layout
// panel1.setLayout(new GridLayout(seat,row));
l = new JButton[4][4]; // Allocating Size of Grid
panel1 = new JPanel(new GridLayout(4,4));
panel1.setBackground(Color.black); …Run Code Online (Sandbox Code Playgroud) 在我点击其中一个编号按钮之前,我只是想让文本字段不可见.忽略我糟糕的间距和缺乏评论,因为这是一个早期的粗略草案.文本字段名为inputField1,我希望当我单击标记为1的按钮时将其设置为Visible.谢谢
import java.lang.String.*;
import java.lang.Exception.*;
import java.util.EventObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Airplanes extends JFrame implements ActionListener {
private static final int FRAME_WIDTH = 330;
private static final int FRAME_HEIGHT = 300;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 300;
private static final int BUTTON_WIDTH = 50;
private static final int BUTTON_HEIGHT = 30;
private JTextField inputLine1;
private JButton oneButton;
private JButton twoButton;
private JButton threeButton;
private JButton fourButton;
private JButton …Run Code Online (Sandbox Code Playgroud) 假设我有一个带有"Exit"内部文本的JMenuItem,以及带有文本"Exit"的JButton,JButton将使用的命令是System.exit(0),当然使用Action Listener,Ok i Know,I can在单击JMenuItem时输入相同的代码,但是没有办法,当我单击JMenuItem时,单击JButton然后执行以下命令(JButton命令)?
我有这个代码
package com.net.Forms;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainForm {
protected static JFrame window = new JFrame("Test Form");
protected static JButton btnOK = new JButton("OK!");
public static void Main() {
load();
return;
}
public static void load() {
window.setSize(500, 500);
window.setVisible(true);
//btnOK.setSize(50, 50); //here
window.add(btnOK);
btnOK.setEnabled(true);
btnOK.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么按钮仍然填充框架而不是如上所述的50 X 50
任何帮助,将不胜感激
我希望我的按钮能够运行一个全新的类,它将在内部执行不同的操作.我不知道这是否可能因为我在java上真的很糟糕.我的代码目前看起来像这样:
public class MainMenu {
private class GardenActivities {
public GardenActivities() {
JFrame GardenAct = new JFrame();
GardenAct.setSize(400, 400);
GardenAct.setVisible(true);
}
}
public static void main(String[] args) {
JFrame choice = new JFrame();
choice.setSize(700, 500);
choice.setLocationRelativeTo(null);
choice.setTitle("Seeds");
choice.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
JButton Labora = new JButton();
Labora.setText("Laboratory");
Labora.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
GardenActivities();
}
});
JButton Garden = new JButton();
Garden.setText("Garden");
Garden.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
} …Run Code Online (Sandbox Code Playgroud) 我一直有问题JFrame.我已经添加了五个JButtons用于我正在处理的RPG程序的用户界面.当"Status"按下按钮时,JFrame冻结,什么也没有,甚至不起作用EXIT_ON_CLOSE.我想知道如何使状态按钮工作,以及如何使用任何其他按钮来避免此问题.
这是ButtonListener班级:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class ButtonListeners {
public static final int WIDTH=360;
public static final int HEIGHT=360;
final static Monsters sk = new Monsters("Skeleton",120,20,30,5,50);
final static Monsters dm = new Monsters("Dark Mage",130,10,40,10,100);
final static Monsters kn = new Monsters("Knight",160,30,40,2,120);
final static Monsters sm = new Monsters("Slime",200,1,50,5,150);
final static Monsters go = new Monsters("golem",500,50,55,15,400); …Run Code Online (Sandbox Code Playgroud) 我的程序中有两个按钮
JButton button1 = new JButton();
button1.setText("First Button");
JButton button2 = new JButton("Second Button");
Run Code Online (Sandbox Code Playgroud)
我试图更改按钮的LaF,我可以使用以下代码更改按钮背景颜色
UIManager.put(Button.background new color(134,201,236));
Run Code Online (Sandbox Code Playgroud)
但是当我试图更改其他键值时"Button.disabled","Button[Default+Focused+Pressed].backgroundPainter"代码对我来说不起作用.有人可以帮我如何更改使用Painter的默认键值吗?
我真的不知道怎么用文字解释这个.所以我有一个带JButton Column的JTable.我为此按钮设置了一个图标,但是当我按下或按住按钮时,它会变为您在下面看到的图像.

如何禁用此效果?我试过以下无济于事:
renderButton.setContentAreaFilled(false);
renderButton.setIcon(new ImageIcon(ButtonColumn.class
.getResource("/com/graphics/clear_left.png")));
renderButton.setRolloverIcon(new ImageIcon(ButtonColumn.class
.getResource("/com/graphics/clear_left.png")));
renderButton.setRolloverSelectedIcon(new ImageIcon(ButtonColumn.class
.getResource("/com/graphics/clear_left.png")));
renderButton.setPressedIcon(new ImageIcon(ButtonColumn.class
.getResource("/com/graphics/clear_left.png")));
Run Code Online (Sandbox Code Playgroud)