我正在制作一个相当简单的文本编辑器,我对我的样式按钮有疑问.当我突出显示文本并单击"粗体"按钮时,文本按预期加粗,但我的选择不再可见.我仍然可以解开选择,使其变为斜体或加下划线,但是你无法看到所选内容.所以,我想知道是否有一个设置允许我点击按钮,但保留我的选择?我尝试了一个JMenuItem而不是JButton,这似乎有用,但随后它让我的工具栏看起来很糟糕.示例代码如下.
//frame and pane creation up here
JToolBar tool = new JToolBar();
JToggleButton boldButton = new JToggleButton("Bold");
boldButton.addActionListener(new StyledEditorKit.BoldAction());
tool.add(boldButton);
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
我正在用Java语言开发一个计算器.问题是,我为数字(0,1,2..9)放了十个按钮,我希望当我点击其中一个按钮时,所有按钮都执行相同的鼠标点击功能.可能吗?在netbeans中,它不允许我这样做,或者我无法实现.谢谢你的帮忙.
我有一个电子邮件客户端,它收到新消息,带有传入消息的按钮开始做某事,直到用户点击它看到最新消息.
我试图通过选择,等待然后取消选择使按钮吸引注意力,但这没有任何作用!
do{
button.setSelected(true);
Thread oThread = new Thread() {
@Override
public void run() {
synchronized (this) {
try {
wait(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
button.setSelected(false);
}
};
oThread.start();
}while(true);
Run Code Online (Sandbox Code Playgroud) 嗨伙计们,当点击提交按钮时,我开始增加,当它是2时,我希望它改变图片,当我是4我想要改变到另一张图片但它没有这样做,所以如果有人有想法它会是大.我正在使用eclipse,程序正在编译和运行.这是代码
/** Here is the GUI of the program
* class name SlideShowGui.java
* @author Kiril Anastasov
* @date 07/03/2012
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SlideShowGui extends JPanel implements ActionListener, Runnable
{
JLabel name, comments, images;
JTextField namejtf, commentsjtf, captionjtf;
JButton submit;
ImageIcon pictures;
SlideShowGui()
{
name = new JLabel("Name:");
this.add(name);
namejtf = new JTextField(15);
this.add(namejtf);
comments = new JLabel("Comments:");
this.add(comments);
commentsjtf = new JTextField(15);
this.add(commentsjtf);
submit = new JButton("Submit");
this.add(submit);
submit.addActionListener(this);
pictures = …Run Code Online (Sandbox Code Playgroud) 我是Java swing的第一次用户,这是我第一次尝试使用私有类.
我正在尝试以下代码 -
ActionListener listener = new AddButtonListener();
Run Code Online (Sandbox Code Playgroud)
其中AddButtonListener是实现ActionListener接口的私有类.
private class AddButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
....
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到了一个读取的eclipse错误
不能访问someType类型的封闭实例.必须使用someType类型的封闭实例限定分配(egxnew A(),其中x是someType的实例).
请注意,该类正在someType中的静态main方法中实例化.
为什么会出现这个错误?是因为主要方法是静态的吗?
这段代码有点问题.该actionPerformed方法不起作用.按钮knappStartSalg和knappStartKunde,当我按动按钮没有反应.
应该导入的所有内容都是导入的.
非常感谢任何帮助.
开始上课.
public class Startmeny extends JFrame implements ActionListener
{
public JButton knappStartSalg, knappStartKunde, knappStartInfo, knappStartStatistikk;
public JPanel startmeny()
{
JPanel startpanel = new JPanel();
startpanel.setLayout(new GridLayout(2, 0, 25, 25) );
startpanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
startpanel.setBackground(Color.white);
JButton knappStartSalg = new JButton();
knappStartSalg.setText("Salg");
knappStartSalg.setVerticalTextPosition(JButton.BOTTOM);
knappStartSalg.setHorizontalTextPosition(JButton.CENTER);
knappStartSalg.setIcon(new javax.swing.ImageIcon(getClass().getResource("salg.png")));
knappStartSalg.setIconTextGap(6);
knappStartSalg.setForeground(Color.black);
knappStartSalg.setBackground(Color.white);
knappStartSalg.setBorderPainted(false);
knappStartSalg.addActionListener(this);
startpanel.add(knappStartSalg);
JButton knappStartKunde = new JButton();
knappStartKunde.setText("Kontroll");
knappStartKunde.setVerticalTextPosition(JButton.BOTTOM);
knappStartKunde.setHorizontalTextPosition(JButton.CENTER);
knappStartKunde.setIcon(new javax.swing.ImageIcon(getClass().getResource("heiskontroll.png")));
knappStartKunde.setIconTextGap(6);
knappStartKunde.setForeground(Color.black);
knappStartKunde.setBackground(Color.white);
knappStartKunde.setBorderPainted(false);
knappStartKunde.addActionListener(this);
startpanel.add(knappStartKunde);
JButton knappStartInfo = new JButton();
knappStartInfo.setText("Informasjonsvindu");
knappStartInfo.setVerticalTextPosition(JButton.BOTTOM); …Run Code Online (Sandbox Code Playgroud) 我已经做了大约1到2个小时的谷歌搜索,这应该是一个非常简单的问题
我需要使用程序GUI中的按钮在我的桌面上启动一个txt doc,例如text.txt(C:\ Users\Computer\Desktop\text.txt).
按钮代码很简单.
JButton btnAddButton1 = new JButton("Display ShopEmployee"); // start of button
btnAddButton1.setVisible( false); // removing from sight
contentPane.add(btnAddButton1);
btnAddButton.addActionListener(new creatingOpenFileListener()); // the button is going to create a Person
btnAddButton.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
和动作监听器:
class creatingOpenFileListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我尽我所能,但我只是出于想法,我甚至前往第5页谷歌搜索寻找答案应该是一个简单的问题,任何帮助都会很棒,谢谢.
编辑:我的意思是启动,而不是打开GUI中的内容,我发布它就好像我要去点击它自己.
回答
try
{
Desktop.getDesktop().open(new File("C://Users//Computer//Desktop//text.txt"));
}
catch(java.io.IOException e)
{
System.out.println("IOError");
}
Run Code Online (Sandbox Code Playgroud) 我必须为练习创建一个简单的计算.应该有4个按钮和一个文本字段作为显示.这是我的4个按钮的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JFrameExample {
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("+");
JButton btn4 = new JButton("=");
public void calculator(){
JFrame frame = new JFrame("SIMPLE JAVA CALCULATOR");
frame.setSize(320,320);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
JPanel HeadPanel = new JPanel();
JPanel NumberPanel = new JPanel();
JPanel LabelPanel = new JPanel();
NumberPanel.add(btn1);
NumberPanel.add(btn2);
NumberPanel.add(btn3);
NumberPanel.add(btn4);
NumberPanel.setLayout(new GridLayout(2,2));
frame.add(NumberPanel,BorderLayout.CENTER);
}
public static void main(String[] args) {
JFrameExample jf = new JFrameExample();
jf.calculator();
} …Run Code Online (Sandbox Code Playgroud) 所以我显然遗漏了一些完全明显的东西.我有一个按钮和一个动作助手连接到它.当我单击按钮时,我想运行一个if语句,它接受TextArea的内容并将其与String进行比较.喜欢如此:
String a = "hello";
JTextArea area = new JTextArea("type something");
JButton button = new JButton("Go");
button. [insert actionlistener crap]
//here's the important part:
if (area.getText() == "hello"){
//this will not be executed
}
Run Code Online (Sandbox Code Playgroud)
这真的很奇怪.我甚至通过调试器和那个if语句,这两个项目都是"你好".但它会跳过它!我究竟做错了什么?
编辑:很多人都在说"使用.equals".谁能告诉我为什么?
public class Iconshape implements Icon {
private Color color = Color.RED;
public Iconshape (Color c)
{
this.color = c;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g1 = (Graphics2D) g;
Ellipse2D.Double circle = new Ellipse2D.Double (0, 0, 20, 20);
g1.setColor(color);
g1.fill(circle);
}
@Override
public int getIconWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getIconHeight() {
// TODO Auto-generated method stub
return 0;
}
public Color setColor(Color c)
{ …Run Code Online (Sandbox Code Playgroud)