我想从 JScrollPane 中的滚动条中删除滚动条箭头按钮。我该怎么做?
我试图在按下 JButton 的同时创建一个不同的actionPerformedwhen Shift,但是当我使用时:
event.isShiftDown;
我的程序无法编译,因为它无法识别它。
我在不止一台计算机上遇到问题(这意味着不仅仅是一台计算机给我带来了问题)。我使用 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) import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SimpleExample extends JFrame {
public SimpleExample() {
setTitle("Simple example");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton jb = new JButton("TEST");
jb.setBorderPainted(true);
jb.setBounds(5, 5, 1, 1); ---> This line
add(jb);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SimpleExample ex = new SimpleExample();
ex.setVisible(true);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
只需创建一个首选大小的简单按钮。该setBounds方法似乎不起作用。我哪里错了?
用图片来解释最容易,所以就在这里。这是它现在的样子:

我试图让所有 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) 我正在制作坦克游戏.在我的菜单中,我想将图片用作jbuttons,它们是部分透明的,当它们出现在屏幕上时,透明部分变为白色.
我尝试使用,.setOpaque但这不起作用.我想不出任何其他方法来摆脱白色部分.我一直在寻找堆栈溢出,但没有一种方法似乎有所帮助.谁有想法?
谢谢!
package menu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class MenuPanel extends JPanel implements ActionListener
{
private Button playKnop, highScoreKnop, quitKnop, HTPKnop;
private JTextField naam;
private Image achtergrond;
private Tanks mainVenster;
public static String naam_input;
int x = 95, width = 200, height = 50;
public MenuPanel(Tanks mainVenster)
{
this.mainVenster = mainVenster;
this.setLayout(null);
playKnop = new Button("/buttons/PLAY.png", 350, this);
highScoreKnop = new Button("/buttons/HS.png", 460, this);
HTPKnop = new Button("/buttons/HTP.png", 515, this);
quitKnop = new …Run Code Online (Sandbox Code Playgroud) 我正在使用 SystemLookAndFeel,它在我的按钮周围设置默认边框。
现在我想要一个股票黑色边框,但是当我设置边框时,它只会在默认边框周围添加我的新边框,所以我有两个。
如何在不删除 LookAndFeel 的情况下更改或删除边框?
另外:我正在使用 java 7 和 Win 8.1
我想让文字和图标对齐电梯侧只有这里是代码
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)