在我JPanel,我将a的背景设置JLabel为不同的颜色.我可以看到"测试"这个词,它是蓝色的,但背景根本不会改变.我该怎么把它展示出来?
this.setBackground(Color.white);
JLabel label = new JLabel("Test");
label.setForeground(Color.blue);
label.setBackground(Color.lightGray);
this.add(label);
Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的JTree.因为我匆忙,TreeModel如果不需要,我宁愿不使用.我写了一个SSCCE来揭露这个问题:
有时我会添加一个节点.其他时候我删除它们.当我推送时Add,正确添加了一个节点.当我推Remove,它应该删除节点,但它没有.此外,如果我尝试添加多个节点,树只会保留我添加的第一个节点.
我写了一个更新方法JTree,我首先擦除从根节点挂起的所有节点,然后我看看我必须创建哪些节点和子节点.
除了不使用TreeModel操作树之外,我在这里做错了什么?
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeTest {
private JFrame myFrame;
private JTree myTree;
private JButton addButton, removeButton;
private int numberOfNodes;
private DefaultMutableTreeNode rootNode;
private ArrayList<String> graphicIDS;
private ArrayList<String> graphicInfo;
public static void main (String [ ] args){
new TreeTest();
}
public TreeTest() { …Run Code Online (Sandbox Code Playgroud) 我的按钮工作正常,我是每个按钮的监听器,如下所示:
for(int i = 0; i <= 25; ++i) {
buttons[i] = new Button(Character.toString(letters[i]));
buttons[i].addActionListener(actionListener);
panel1.add(buttons[i]);
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到调用者被调用,我想找出我点击的按钮.有没有办法做到这一点?
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println(actionEvent.getSource());
}
};
Run Code Online (Sandbox Code Playgroud)
我需要一些方法来找到数组中的按钮.
我正在尝试使用"是"或"否"按钮创建消息.然后会出现一个窗口,其中包含某条消息,该消息取决于用户是单击是还是否.
这是我的代码:
public class test{
public static void main(String[] args){
//default icon, custom title
int n = JOptionPane.showConfirmDialog(
null,
"Would you like green eggs and ham?",
"An Inane Question",
JOptionPane.YES_NO_OPTION);
if(true){
JOptionPane.showMessageDialog(null, "HELLO");
}
else {
JOptionPane.showMessageDialog(null, "GOODBYE");
}
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
现在它会打印HELLO,无论你是否按下否.当用户选择否时,如何让它显示GOODBYE?
当用户输入JTextArea类似手机T9时,我需要显示建议(自动完成).
我不知道如何做到这一点myTextAreaKeyTyped().
这个应用程序是一个打字助手.它显示键盘上不存在的字符变体.EG你按'A',它显示Â:1,Á:2,À:3 ......如果按1,2或3,'A'将被替换.它已经完成,但是变量显示在JLabel底部a我的JFrame,因为我不知道该怎么做.
你能帮帮我吗?提前致谢.
我有两个JButton文本"Ok"和"Cancel".我用它GridBagLayout来对齐它们JDialog.我把锚设置为GridBagConstraints.CENTER.由于文本"Ok"和"Cancel"中字符数的不同,按钮的大小不同.如何正确对齐它们,使它们每个都具有相同的大小.我试过以下但没有用.
okayButton.setSize(cancelButton.getSize());
Run Code Online (Sandbox Code Playgroud) 我正在尝试用Java创建一个应用程序.为了使Swing正常工作,我这样做了:
public static void main(String[] array){
String outerInput;
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
// I want this string input.
String input = JOptionPane.showInputDialog(
null,"Stop ?", JOptionPane.QUESTION_MESSAGE);
});
// How can I get this input value in String outerInput?
}
Run Code Online (Sandbox Code Playgroud)
我如何在我的主体中获得此输入字符串?
我正在尝试使用空列作为a中列对之间的分隔符JTable.这是我到目前为止的图片和代码.我知道我可以使用自定义更改外观TableCellRenderer.在我走这条路之前,还有更好的方法吗?任何想法都赞赏.
TablePanel.png http://i42.tinypic.com/1zxpfkj.png
import javax.swing.*;
import javax.swing.table.*;
public class TablePanel extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("TablePanel");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.add(new TablePanel());
f.pack();
f.setVisible(true);
}
});
}
public TablePanel() {
TableModel dataModel = new MyModel();
JTable table = new JTable(dataModel);
table.getColumnModel().getColumn(MyModel.DIVIDER).setMaxWidth(0);
JScrollPane jsp = new JScrollPane(table);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(jsp);
}
private static class MyModel extends AbstractTableModel {
private static final int DIVIDER = 2; …Run Code Online (Sandbox Code Playgroud) 我正在使用LWJGL开发Java游戏.与主游戏一起,我正在开发一些基于Swing的应用程序(启动器,配置编辑器和地图编辑器).
当我运行启动器时,我有时(大约10%的时间)会遇到致命的JVM崩溃.这很奇怪 - 我可以在几秒钟后运行该程序,一切正常.我没有编译错误或警告,当程序运行时,它完全正常.
当我运行LWJGL游戏时,我没有得到错误,只有当我首先运行启动器时才会出现错误.我用来初始化启动器的过程是:
我得到的错误如下:
# # A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007fefd800c7b, pid=6128, tid=4364 # # JRE version: 6.0_27-b07 # Java VM: Java HotSpot(TM) 64-Bit Server VM (20.2-b06 mixed mode windows-amd64 compressed oops) # Problematic frame: # C [ole32.dll+0x10c7b] # # An error report file with more information is saved as: # J:\Development\workspace\Crypt Utils\hs_err_pid6128.log # # …
您好,我有以下问题:
public class TestCombo extends JFrame{
public TestCombo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,200);
setVisible(true);
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,4));
JLabel l1 = new JLabel("test1");
JLabel l2 = new JLabel("test2");
panel.add(l1);
panel.add(l2);
// JComboBox<String> combo = new JComboBox<String>();// <-- uncomment this for the problem
this.add(panel, BorderLayout.NORTH);
}
public static void main(String[] args) {
new TestCombo();
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在做一个非常简单的例子.如果我取消注释标记的部分,则不显示标签项.如果我调整窗口大小,它们会再次可见.这里奇怪的是我甚combo至没有添加到面板或任何地方.我只是在实例化它.有人能告诉我为什么我需要调整框架大小以查看标签吗?难道我做错了什么?
java ×10
swing ×10
button ×1
combobox ×1
divider ×1
fatal-error ×1
input ×1
jbutton ×1
jlabel ×1
joptionpane ×1
jtable ×1
jtextarea ×1
jtree ×1
jvm ×1
lwjgl ×1
render ×1
return-value ×1
tooltip ×1
tree-nodes ×1
treemodel ×1