是否可以在Swing中将Button叠加在Label上?
例如,如果有一个带有图像的JLabel而没有文本,我想在我的JLabel上覆盖我的按钮.标签定义如下:
myLabel = new javax.swing.JLabel(new ImageIcon( myPicture ));
Run Code Online (Sandbox Code Playgroud)
如果没有,那么任何想法我怎么能意识到这一点,谢谢.
编辑:其实我读到有关添加JPanel到JLabel,当我添加一个面板按钮布局,它编译得很好,但没有任何可见,只是JLabel与图像
更新:正如@ paranoid-android所建议的那样,我已经解决了我的问题.然而,我仍然必须知道如何自定义覆盖在JLabel顶部的组件的位置,因为我没有太多的控制(可能因为通常我使用netbeans绘制布局,这将需要硬编码).
像这样的东西有效:
ImagePanel(Image image, int id) {
this.image = image;
this.tile = false;
JButton backButton = new JButton();
JButton nextButton = new JButton();
backButton.setText(" BACK ");
nextButton.setText(" NEXT ");
add(backButton);
add(nextButton);
};
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
Run Code Online (Sandbox Code Playgroud) 我有类似下面的代码:
for(int i=0;i<10;i++){
button=new JButton(buttons[i]);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
setPage(i);
}
});
menu.add(button);
}
Run Code Online (Sandbox Code Playgroud)
但是,该变量i未在ActionListener类的范围内定义.我怎样才能传递变量?
我正在尝试创建一个带有 JList 和按钮的表单,我想要做的是从 JList 中选择一个项目,然后按按钮执行取决于选择的操作。但是,一旦单击按钮,JList 就会失去焦点,并且选择也会消失,从而使按钮无法确定在 JList 中选择了哪个元素。
有针对这个的解决方法吗?提前致谢 :)
有人有 SplitButton 的编码实现吗?
我尝试搜索一些实现,但没有一个起作用。(例如JSplitButton)。
我正在使用 Java 创建一个 Tic-Tac-Toe 游戏。现在,我有了它,因此当您单击一个按钮时,该按钮JButton将从 中删除JPanel,JLabel添加一个包含 X 或 O 图像的图像,并且JPanel将重新绘制。但是,当我单击该按钮时,图像不会显示,但按钮消失。
创建按钮和JLabel/ Image:
package tictactoe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
public class TicTacToe implements ActionListener
{
private JFrame holder = new JFrame();
private GridLayout layout = new GridLayout(3,3);
private FlowLayout panel = new FlowLayout(FlowLayout.CENTER);
private JPanel p11, p12, p13, p21, p22, p23, p31, p32, p33;
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
private …Run Code Online (Sandbox Code Playgroud) 您好我正在尝试为JButton实现Action侦听器,代码如下所示:
ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
one = new JButton("",imageForOne);
one.setPreferredSize( new Dimension(78, 76));
one.addActionListener(myButtonHandler);
Run Code Online (Sandbox Code Playgroud)
使用上面的JButton看起来很好
当我为按钮添加特定值时,例如
ImageIcon imageForOne = new ImageIcon(getClass().getResource("resources//one.png"));
//Check this
one = new JButton("one",imageForOne);
one.setPreferredSize( new Dimension(78, 76));
one.addActionListener(myButtonHandler);
Run Code Online (Sandbox Code Playgroud)
它看起来像下图

有什么办法可以避免这种情况并设定价值.
感谢您的帮助.
按下时,任何按下的 JButton 似乎都会“突出显示”自身,如下所示:

我似乎找不到任何方法来禁用它。
我在 JButton 中使用Font Awesome创建一个可单击的图标,但是当尺寸较小时,生成的图标会出现别名。作为一点背景知识,Font Awesome是一个可下载的ttf文件(字体文件),其中每个字符都是一个“可缩放矢量图标”。paintComponent查看了 Google 和堆栈溢出之前的答案后,我尝试通过覆盖JButton 的方法来强制抗锯齿;然而这似乎没有效果:
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test extends JFrame{
public Test(){
Font fontAwesome = null;
try {
fontAwesome = Font.createFont(Font.TRUETYPE_FONT, new File("font-awesome-4.2.0\\fonts\\fontawesome-webfont.ttf"));
fontAwesome = fontAwesome.deriveFont(Font.PLAIN, 100);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
JButton iconButton = new JButton("\uf0a8"){
@Override
public void paintComponent(Graphics g) {
Graphics2D graphics2d = (Graphics2D) g;
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, …Run Code Online (Sandbox Code Playgroud) 我有一个 JButton,其背景为绿色,边框为 LineBorder。我想在按钮和边框之间插入一个空格,一种填充。我试过 setMargin(new Insets(x,y,t,z)) 但它似乎不起作用。这是我的一段代码。
JButton JBtn=new JButton("sdfd");
JBtn.setBorder(BorderFactory.createLineBorder(Color.CYAN,5));
JBtn.setBackground(Color.GREEN);
JBtn.setMargin(new Insets(5,5,10,10));
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
以下代码呈现JButton无文本:
public abstract class Test {
public static void main(String... args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String text = "Invisible";
JButton button = new JButton(text); // blank button rendered ???
System.out.println(button.getText()); // prints Invisible
button.setAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
// do nothing
}
});
System.out.println(button.getText()); // prints null ??? …Run Code Online (Sandbox Code Playgroud)