我想从 JScrollPane 中的滚动条中删除滚动条箭头按钮。我该怎么做?
可能重复:
多次通过单击摆动按钮更改标签文本无法正常工作
我正在使用Java Swing开发一个GUI,其中我使用两个组件JButton和JLabel.JLabel的文本最初设置为"单击按钮".单击按钮后,我希望JLabel的文本更改为"Processing".最后到"Processed"
因此,当我单击按钮时,控件转到ActionPerformed,其中我使用setText()方法将JLabel的文本设置为"Processing".ActionPerformed中的最后一个语句是使用setText()将JLabel的文本设置为"Processed".
当我运行程序时,标签显示"单击按钮".最后它变为"已处理".但是,它从不显示"正在处理".
我创建了一个接收Action的JButton类,JButton类包括击键和鼠标监听器,因此我可以根据需要在多个帧中使用相同的类.
我的问题是:按下键时JButton没有得到焦点,但是它正在执行操作.我需要创建一个新的背景或告诉用户按钮执行操作的东西.
有任何想法吗??
这是我的代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import swtdesigner.SwingResourceManager;
public class IButtonSave extends JButton{
private static final long serialVersionUID = 1L;
private Action action = null;
public IButtonSave() {
super();
setFocusPainted(true);
setFocusable(true);
try {
jbInit();
} catch (Throwable e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
setMargin(new Insets(0, 0, 0, 0));
setBorder(new LineBorder(Color.black, 1, true));
setIconTextGap(0); …Run Code Online (Sandbox Code Playgroud) 我在不止一台计算机上遇到问题(这意味着不仅仅是一台计算机给我带来了问题)。我使用 Eclipse 作为 Java IDE,但我似乎无法找到问题的答案。我已将几个 JButton 添加到视频游戏的启动器中,当我调试项目时,这些按钮不会出现,除非我将鼠标滑过它们,或者最小化并重新打开窗口。我已经导出了项目,大部分按钮都可以工作,但有时我仍然需要最小化并重新打开窗口。我不想这样做,因为这将是一款商业视频游戏,人们不应该这样做。
如果您需要它,这是我的启动器窗口的代码:
package net.renderedspoon.sombrero.gui;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import net.renderedspoon.sombrero.Configuration;
import net.renderedspoon.sombrero.Game;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import net.renderedspoon.sombrero.RunGame;
public class Launcher extends JFrame {
private static final long serialVersionUID = 1L;
protected JPanel window = new JPanel();
private JButton play, options, help, exit;
private Rectangle rplay, roptions, rhelp, rexit;
Configuration config = new Configuration();
private int width = 250;
private int height = 350;
protected int …Run Code Online (Sandbox Code Playgroud) 用图片来解释最容易,所以就在这里。这是它现在的样子:

我试图让所有 JButton 的大小相同,即完全垂直填充 BoxLayout。
这是我的代码:
public class TestarBara extends JFrame implements ActionListener{
JButton heyy;
public static void main(String[] args){
new TestarBara();
}
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
public TestarBara(){
super("knapparnshit");
panel.setLayout(new GridLayout(3,3,2,2));
for(int x=1; x < 10; x++){
String y = Integer.toString(x);
JButton button = new JButton(y);
button.addActionListener(this);
panel.add(button);
}
add(panel, BorderLayout.CENTER);
JButton b1 = new JButton("this");
JButton b2 = new JButton("does");
JButton b3 = new JButton("not");
JButton b4 = new JButton("work");
panel2.setLayout(new BoxLayout(panel2, …Run Code Online (Sandbox Code Playgroud) 主类:
public class tuna {
public static void main(String[] args) {
JFrame frame1 = new JFrame();
frame1.setVisible(true);
frame1.add(new apple());
frame1.setSize(200 , 240);
}
}
Run Code Online (Sandbox Code Playgroud)
二级
public class apple extends JPanel{
JTextArea ta = new JTextArea();
Border blackline = BorderFactory.createLineBorder(Color.black);
apple(){
setBorder(blackline);
System.out.println("apple");
ta.setText("hello");
ta.setEditable(false);
add(ta);
add(new doctor());
repaint();
revalidate();
}
}
Run Code Online (Sandbox Code Playgroud)
三级
public class doctor extends JPanel implements ActionListener{
public JButton butt = new JButton("change");
Border blackline = BorderFactory.createLineBorder(Color.black);
public doctor(){
setBorder(blackline);
add(butt);
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource() …Run Code Online (Sandbox Code Playgroud) 我正在使用 SystemLookAndFeel,它在我的按钮周围设置默认边框。
现在我想要一个股票黑色边框,但是当我设置边框时,它只会在默认边框周围添加我的新边框,所以我有两个。
如何在不删除 LookAndFeel 的情况下更改或删除边框?
另外:我正在使用 java 7 和 Win 8.1
我在这里不允许出现错误'void'类型
码:
public void create(JPanel jp){
jp.add(new JButton().setPreferredSize(new Dimension(40, 40)));
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用相同的代码而没有.setPreferredSize(new Dimension(40, 40))它工作正常.
工作代码
public void create(JPanel jp){
jp.add(new JButton());
}
Run Code Online (Sandbox Code Playgroud) 我想让文字和图标对齐电梯侧只有这里是代码
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LeftSide
{
public LeftSide()
{
JFrame frame = new JFrame("Button");
JPanel panel = new JPanel();
JButton button = new JButton("Submit");
button.setPreferredSize(new Dimension(200, 30));
button.setIcon(new ImageIcon(this.getClass().getResource("submit.gif")));
panel.add(button);
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args)
{
new LeftSide();
}
Run Code Online (Sandbox Code Playgroud)
}
如果我运行此代码,我将在按钮中心的按钮上获得图标和文本,因此如何使它们位于左侧;
我想在 Java Swing 中更改 JButton 组件的边框颜色。
我尝试了以下方法:
package com.example.test;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test extends JFrame {
public Test() {
JPanel panel = new JPanel();
JButton button1 = new JButton("Test Button 1");
JButton button2 = new JButton("Test Button 2");
button2.setBorder(BorderFactory.createLineBorder(Color.RED));
panel.add(button1);
panel.add(button2);
this.add(panel);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (ClassNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, …Run Code Online (Sandbox Code Playgroud)