我是一名初学者,正在构建一个带按钮和滚动条的简单窗口.当我编译我的代码时,我的按钮上的文字被删除了一个省略号,图像图标没有显示.我试过在eclipse和NetBeans中编译它.为了解决我试过的问题
.setMargin(new Insets(0, 0, 0, 0));
.setPreferedSize
adding padding (I forgot the code for this)
.setBounds
Run Code Online (Sandbox Code Playgroud)
以及我在互联网上偶然发现的其他一切.这些都没有解决我的问题,我无法查看按钮中的文本和图像.
我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FeedBar2 extends JFrame {
public FeedBar2() {
super("FeedBar 2");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create icons
ImageIcon loadIcon = new ImageIcon("load.gif");
ImageIcon saveIcon = new ImageIcon("save.gif");
ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
// create buttons
JButton load = new JButton("Load", loadIcon);
JButton save = new JButton("Save", saveIcon);
JButton subscribe = new JButton("Subscribe", subscribeIcon); …Run Code Online (Sandbox Code Playgroud) 我试图自动增加/减少JButton文本的字体大小(如果JButton增加/延伸其文本也将增加,如果JButton减少其文本也将减少).JButtons的默认字体是Sans-Serif大小为20,它永远不会减少到20以下(它可以是21,30,40或任何高于或等于20但不低于20的任何东西).我有一个名为MenuJPanel的JPanel,它使用GridLayout添加5个JButton,随着JPanel的增加/减少,这将增加/减少.我选择了GridLayout,因为它似乎是为此目的的最佳布局,我错了吗?我还在MenuJPanel中添加了一个componentResized.下面你可以看到我的代码部分工作.

public class MenuJPanel extends JPanel {
private JButton resizeBtn1;
private JButton resizeBtn2;
private JButton resizeBtn3;
private JButton resizeBtn4;
private JButton resizeBtn5;
public MenuJPanel() {
initComponents();
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Font btnFont = resizeBtn1.getFont();
String btnText = resizeBtn1.getText();
int stringWidth = resizeBtn1.getFontMetrics(btnFont).stringWidth(btnText);
int componentWidth = resizeBtn1.getWidth();
// Find out how much the font can grow in width.
double widthRatio = (double) componentWidth / (double) stringWidth;
int newFontSize = (int) (btnFont.getSize() * widthRatio);
int componentHeight = …Run Code Online (Sandbox Code Playgroud) 我想在Java中创建圆形JButton ...
为此,我使用圆形图像并将该图像放在按钮上,但我没有得到圆形按钮..
请任何人可以告诉如何在Java中创建圆形按钮如下图所示..

提前致谢.....
我有一个名为ImageButton的自定义按钮类,它扩展了JButton.在其中我有一个我想要调用的setEnabled方法而不是JButton的setEnabled方法.
我的代码如下.在我的另一个类中,我创建了一个ImageButton的新实例,但是当我尝试使用setEnabled方法时,它会直接进入JButton的setEnabled方法.甚至在我运行代码之前,我的IDE就告诉我从未使用过ImageButton的setEnabled方法.如果我将方法更改为"SetOn",它可以正常工作.那么为什么我不能使用与超类相同的名字呢?我认为它应该隐藏超类方法,如果它是相同的名称?
public class ImageButton extends JButton{
public ImageButton(ImageIcon icon){
setSize(icon.getImage().getWidth(null),icon.getImage().getHeight(null));
setIcon(icon);
setMargin(new Insets(0,0,0,0));
setIconTextGap(0);
setBorderPainted(true);
setBackground(Color.black);
setText(null);
}
public void setEnabled(Boolean b){
if (b){
setBackground(Color.black);
} else {
setBackground(Color.gray);
}
super.setEnabled(b);
}
}
Run Code Online (Sandbox Code Playgroud) 所以我的GUI有一小段代码:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class IPGUI extends JFrame implements ActionListener
{
private static JPanel contentPane;
private static boolean btn1Clicked = false;
private static boolean btn2Clicked = false;
private static boolean btn3Clicked = false;
private static boolean btn4Clicked = false;
//Create the frame
public IPGUI()
{
//Sets frame properties
setTitle("IP Extractor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new …Run Code Online (Sandbox Code Playgroud) 我已经添加了一个按钮JPanel.如果JPanel包含按钮,我想删除按钮.有没有办法检查是否JPanel包含按钮?
我正在使用Mac上的Eclipse设计GUI,我想让我JButton只显示我设置的Icon.它在Mac OS上看起来很好,但是当我将它导出到jar文件并在Windows操作系统上运行时,JButton看起来像这样:

边界都是EmptyBorder.
我做错了什么?怎么能让后面的广场消失?
我正在寻找一种方法将变量或字符串或任何东西传递给JButton的匿名actionlistener(或显式actionlistener).这是我有的:
public class Tool {
...
public static void addDialog() {
JButton addButton = new JButton( "Add" );
JTextField entry = new JTextField( "Entry Text", 20 );
...
addButton.addActionListener( new ActionListener( ) {
public void actionPerformed( ActionEvent e )
{
System.out.println( entry.getText() );
}
});
...
}
}
Run Code Online (Sandbox Code Playgroud)
现在我只是声明entry是一个全局变量,但我讨厌这样做的方式.还有更好的选择吗?
我目前是一个java新手,编写一个程序,基本上是一个棋盘游戏,几乎与跳棋相同.不幸的是,这也意味着我需要一百个JButtons用于棋盘.我知道有一种方法可以自动生成JButton,我已经看过它了.不过,我不知道怎么会这样做.任何帮助,将不胜感激!
我是Java Swing的新手,我遇到了一些问题.
因此,如下面的代码所示,我有一个JFrame,我调用MainPanel类来创建一个面板并添加一个带按钮的ToolBar.按下按钮时,它会向面板添加一个按钮.在您调整窗口大小之前单击按钮时没有显示问题(在我的情况下,我只需手动拖动屏幕使其变大).
public class Main {
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MathMaker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create the menu bar. Make it have a green background.
//MainToolBar mainTB = new MainToolBar();
MainPanel mainPanel = new MainPanel();
frame.getContentPane().add(mainPanel.getGUI(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
} …Run Code Online (Sandbox Code Playgroud)java ×10
jbutton ×10
swing ×9
jpanel ×3
grid-layout ×1
imageicon ×1
inheritance ×1
jcomponent ×1
jscrollpane ×1
jtoolbar ×1
methods ×1