我正在Java Swing中创建一个虚拟钢琴类型程序.我现在的钢琴键区域是一个JPanel,其水平BoxLayout包含白色JButtons作为白键.我也想添加黑键,让它们与白键重叠.
我尝试过两种不同的方法.一个是使用OverlayLayout.遗憾的是,OverlayLayout管理器在线没有太多文档,并且在NetBeans GUI构建器中不可用.我不知道如何让它发挥作用.我尝试过的第二件事是使用JLayeredPanes.即使在Netbeans中弄乱它之后,我似乎无法想出那个.
所以我认为我的问题非常简单.如果有的话,在其他JButton之上添加JButton的最佳方法是什么?或者也许有一个替代使用JButtons钢琴键?
编辑
我结合了aioobe和dacwe的代码来获得我想要的结果.我基本上使用dacwe的z-ordering与aioobe的基本尺寸(按比例放大)和mod 7部分.我还添加了一些变量以使事情更加清晰.这就是我现在拥有的.
import javax.swing.*;
import java.awt.Color;
public class Test2 {
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
JLayeredPane panel = new JLayeredPane();
frame.add(panel);
int maxKeys = 8;
int width = 60;
int height = 240;
for (int i = 0; i < maxKeys; i++) {
JButton b = new JButton();
b.setBackground(Color.WHITE);
b.setLocation(i * width, 0);
b.setSize(width, height);
panel.add(b, 0, -1);
}
int width2 = 48;
int height2 = 140;
for …Run Code Online (Sandbox Code Playgroud) 我正在尝试修复一个JFrame,其中将有一个背景图像和图像JButtons将执行一些命令.我尝试在没有布局的情况下这样做,因为我想在JFrame上的某些特定位置放置小按钮,但每次我这样做时,背景图像都会出现在前面,或者JFrame的大小等于JFrame大小.使用以下代码,JButton与JFrame具有相同的大小.我试图改变JButton的大小和位置,但没有.你能帮我吗?
这是代码
public final class Test extends JComponent
{
private Image background;
private JFrame frame;
private Dimension dimension;
public Test()
{
dimension = new Dimension(15, 15);
frame = new JFrame("Iphone");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.setBounds(641, 0, 344, 655);
frame.setVisible(true);
test = displayButton("tigka");
frame.getContentPane().add(test);
}
public void update(Graphics g)
{
paint(g);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(background, 0, 25, null); // draw background
Run Code Online (Sandbox Code Playgroud)
// 标签();
test = displayButton("test"); public JButton displayButton(String name){JButton button = new JButton(name); button.setSize(100,100); button.setPreferredSize(尺寸); 返回按钮; }
我的GUI中有一个部分是根据对象列表动态生成的.因此,对于该列表中的每个对象,我想创建一个JButton并关联一个键盘快捷键.
例如:
for (String tag : testTags) {
new JButton(tag).setMnemonic(KeyEvent.VK_F1);
}
Run Code Online (Sandbox Code Playgroud)
如何以优雅的方式使代码"setMnemonic(KeyEvent.VK_F1)"动态化?有没有办法自动获取一系列键,然后在此迭代中使用它?
谢谢!
这一直困扰着我.如果我在定义之前setText在JButton上定义,文本将消失:setAction
JButton test = new JButton();
test.setText("test"); // Before - disappears!
test.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// do something
}
});
this.add(test);
Run Code Online (Sandbox Code Playgroud)
如果它之后,没有问题.
JButton test = new JButton();
test.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// do something
}
});
test.setText("test"); // After - no problem!
this.add(test);
Run Code Online (Sandbox Code Playgroud)
此外,如果我在JButton构造函数中设置文本,那很好!Yarghh!
为什么会这样?
我如何在每一行显示JOptionPane.showinputDialog()多个JButtons?我不是在谈论Yes,No,Cancel按钮,但多个自定义标记JButtons,它显示的内容区域JOptionPane.showinputDialog?
所以我需要从按钮中获取按钮的值JOptionPane.
好吧,所以我试图从我用来学习Java的书中做练习.这是我到目前为止的代码:
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
public class Calculator {
//Declaration of all calculator's components.
JPanel windowContent;
JTextField displayField;
JButton button0;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton buttonPoint;
JButton buttonAdd;
JButton buttonEqual;
JPanel pl;
//Constructor creates the components in memory and adds the to the frame using combination of Borderlayout.
Calculator() {
windowContent= new JPanel();
// Set the layout manager for this panel
BorderLayout bl = …Run Code Online (Sandbox Code Playgroud) 基本上,我在主代码中的某处调用了这行代码
editwindow clubeditwindow = new editwindow(1,"Club Edit");
Run Code Online (Sandbox Code Playgroud)
这一行打开一个新的JFrame,它基本上可以编辑主类中的一堆信息.我有两个按钮,称为保存和取消.单击保存时,我想从文本字段中获取值,然后将其放入新对象并将其返回到主类,然后关闭窗口.点击取消后,我希望它不做任何事情,这很简单.
提前致谢.
我有一个JFrame2 JPanel:a PaintPanel(带paint()方法)和a ButtonPanel(带按钮).当我调用repaint()的PaintPanel(但点击该按钮)的按钮ButtonPanel被涂在PaintPanel!它不是可点击的或任何东西,它就在那里.
我尝试使用此代码重新创建问题:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("frame");
frame.setSize(400,400);
frame.setLayout(new GridLayout(2,1));
PaintPanel paint = new PaintPanel();
ButtonPanel buttons = new ButtonPanel(paint);
frame.add(paint);
frame.add(buttons);
frame.setVisible(true);
}
}
public class PaintPanel extends JPanel{
public void paint(Graphics g){
g.drawRect(10, 10, 10, 10);
}
}
public class ButtonPanel extends JPanel implements ActionListener{
private PaintPanel paintPanel;
public ButtonPanel(PaintPanel …Run Code Online (Sandbox Code Playgroud) 我有两个JButtons叫做"左"和"右"."向左"按钮向左移动一个矩形对象,"向右"按钮向右移动矩形对象.我ActionListener在类中有一个作为单击任一按钮时的监听器.但是,我希望在单击每个动作时发生不同的操作.我如何区分,ActionListener点击之间?
我有一个JButton,我想将背景设置为一种颜色。
JButton button = new JButton();
button.setVisible(true);
button.setPreferredSize(new Dimension(student_scroll.getWidth(), 50));
button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
button.setOpaque(true);
Run Code Online (Sandbox Code Playgroud)
我在Mac上使用了它,并且按我的意愿出现了。但是,在Windows上尝试时,前景为白色(应有),但背景为空。
说添加button.setContentAreaFilled(false);我做了但没有效果的。大多数其他人说添加button.setOpaque(true);我也已经做过的。
我还必须做些什么才能使它显示为黑色背景?
编辑
根据要求,SSCCE:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MainSwing extends JFrame {
private static final long serialVersionUID = -8231889836024827530L;
public static void main(String[] args) {
try {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");
UIManager.put("ScrollBarUI", "main.CustomScrollBarUI");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException: " + e.getMessage());
} …Run Code Online (Sandbox Code Playgroud)