我有一个半透明的 JPanel.我通过扩展JButton 创建了一个自定义的JButton,因为我需要一个带圆角的按钮,并希望为它添加一些效果.我已经使按钮不透明.当我将这个按钮添加到我的半透明JPanel时,它很好.但是在翻滚时,按钮后面会画一个黑色的补丁,看起来很糟糕.我在网上寻找解决方案但却找不到有用的解决方案.这个问题也在http://www.java.net/node/661798中描述, 但我无法真正让kirillcool的建议工作.....任何帮助将不胜感激
我有一个JButton名为button1的文本"Alter Today".
我想在这个按钮的'今天'一词的'T'下设置一个助记符(这是'今天改变'中't'或'T'的第二个例子).
当我想要做的时候:
button1.setMnemonic(6);
Run Code Online (Sandbox Code Playgroud)
我无法看到带下划线的'T'.
我在做的时候:
button1.setMnemonic('T');
Run Code Online (Sandbox Code Playgroud)
它仍然在'Alter'这个词中强调't'.
怎么做?
我试图找到答案,但我很简短.我对java很新.我有4个类(1个主要有一个JFrame和3个JPanels).主类创建JFrame并向其添加3个面板.我想要完成的是从另一个面板(panelB)中的ActionEvent更新1面板(panelA)中的JLabel或JTextField.panelB中的ActionEvent在panelA中运行一个方法,该方法运行setText()方法和repaint()方法.我无法使用新文本更新JLabel或JTextField.
这是我的代码:
public class App {
public static void main(String[] args) {
JFrame nameFrame = new JFrame("Name Form");
nameFrame.setLayout(new BorderLayout());
nameFrame.setSize(300,150);
nameFrame.setLocationRelativeTo(null);
nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MiddlePanel middlePanel = new MiddlePanel();
nameFrame.add(middlePanel, BorderLayout.CENTER);
BottomPanel bottomPanel = new BottomPanel();
nameFrame.add(bottomPanel, BorderLayout.SOUTH);
nameFrame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class BottomPanel extends JPanel {
private JLabel welcomeLabel = new JLabel("Old Text");
BottomPanel() {
super(new FlowLayout());
add(welcomeLabel);
}
public void setText(String text) {
welcomeLabel.setText(text);
repaint();
}
}
Run Code Online (Sandbox Code Playgroud)
import java.awt.BorderLayout;
import …Run Code Online (Sandbox Code Playgroud) 我试图在单击JButton时显示JLabel.我添加了一个动作侦听器并将该组件添加到布局中.在actionPerformed中单击JButton时,我正在使用label1.setVisible(true).我仍然无法使它工作.有人可以看看我的代码吗?
public class LearnAppMain extends JFrame implements ActionListener {
// Define variables
public JButton button1;
public JLabel label1;
public JTextField field1;
private Image image1;
private String apple = "apple.jpg";
public LearnAppMain() {
ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple));
JLabel label1 = new JLabel(image1);
button1 = new JButton("A");
button1.addActionListener(this);
field1 = new JTextField(10);
// Create layout
setLayout(new FlowLayout());
// create Container
final Container cn = getContentPane();
cn.add(button1);
cn.add(field1);
cn.add(label1);
// setLayout(new FlowLayout());
setSize(250, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent …Run Code Online (Sandbox Code Playgroud) 我想为JButton添加一个KeyEventListener,它响应Enter键,使用以下代码段:
private void jButton3KeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == 10) {
eventRegister();
}
}
Run Code Online (Sandbox Code Playgroud)
我按空格键而不是输入,if条件设置为true并eventRegister调用.为什么?我怎么能阻止这种方式?
我正在尝试使用swing编写Tic Tac Toe程序,但我似乎遇到了一些麻烦.在我的匿名内部类中,我尝试为每个按钮设置actionListener,但是我无法找到允许我引用按钮并将它们设置为X或Y的类型或变量.我试过了.getSource().setText()在我的匿名类中,但是返回时出现了错误.有什么想法吗?谢谢!亚历克斯
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TicTacToe {
public JFrame frame;
public JLabel label;
public JPanel panel;
public static int counter;
public void go()
{
frame = new JFrame("TicTacToe");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridLayout(3,3,10,10));
frame.add(BorderLayout.CENTER, panel);
label= new JLabel("TIC TAC TOE");
frame.add(BorderLayout.NORTH, label);
;
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 1");
JButton button3 = new JButton("Button 1");
JButton button4 = new JButton("Button 1");
JButton …Run Code Online (Sandbox Code Playgroud) 如何检查JButton是否被按下?我知道有一个方法,它的名字是"isEnabled"
所以我尝试编写一个代码进行测试.
这里的代码:
final JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
panel.add(btnAdd);
JButton btnConfirm = new JButton("Check Out");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnAdd.isEnabled()) {
System.out.println("Add Button is pressed");
}
if (!btnAdd.isEnabled()) {
System.out.println("Add Button is not pressed");
}
}
});
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,代码只提供"按下添加按钮",虽然我没有按"添加"按钮.为什么会这样发生?
我的任务是检索文本字段的值,并在单击按钮时将其显示在警告框中.如何在java swing中为按钮生成on click事件?
我正在为我的项目创建一个gui.首次加载gui时,只能看到背景,因此按钮不可见,但当鼠标悬停在它们上面时,它们是可见的.有什么办法解决这个问题?
public class Home extends JFrame{
//New JPanel
private JPanel home;
//Creating image url. You must be change url
ImageIcon icon = new ImageIcon("img//home1.jpeg");
//Home Class
public Home(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 960, 640);
setTitle("LoneyTunes Crush");
home = new JPanel();
home.setBorder(new EmptyBorder(5, 5, 5, 5));
home.setLayout(new BorderLayout(0, 0));
setContentPane(home);
getContentPane().setLayout(null);
JLabel background = new JLabel(new ImageIcon("img//giphy."));
getContentPane().add(background);
background.setLayout(new FlowLayout());
//Creating Buttons
JButton play = new JButton("Play");
play.setBounds(20, 20, 200, 30);
JButton setting = new JButton("Settings");
setting.setBounds(20, 60, 200, 30);
JButton exit …Run Code Online (Sandbox Code Playgroud) java ×10
jbutton ×10
swing ×10
jlabel ×2
awt ×1
awtutilities ×1
background ×1
constructor ×1
keylistener ×1
mnemonics ×1
paint ×1
tic-tac-toe ×1
translucency ×1