天道酬勤,学无止境

jpanel

Java获取JPanel组件(Java get JPanel Components)

问题 我有一个充满 JTextFields 的 JPanel ...... for (int i=0; i<maxPoints; i++) { JTextField textField = new JTextField(); points.add(textField); } 我以后如何在该 JPanel 中获取 JTextFields? 就像我想要他们的价值观一样 TextField.getText(); 谢谢 回答1 请记住,他们自己并没有到达那里(我认为阅读一些有关在运行时动态创建这些面板的问题) 在那里发布的答案中,有人说您应该在数组中保留对这些文本字段的引用。 这正是您所需要的: List<JTextField> list = new ArrayLists<JTextField>(); // your code... for (int i=0; i<maxPoints; i++) { JTextField textField = new JTextField(); points.add(textField); list.add( textField ); // keep a reference to those fields. } // 之后 for( JTextField f : list ) { System.out.println( f.getText() ) ; }

2021-06-24 05:46:51    分类:技术分享    java   swing   jframe   jpanel   jcomponent

按下 JButton 时 JFrame 不添加组件(JFrame does not add component when JButton is pressed)

问题 我的代码基本上是关于有一个框架和一个按钮。 您按下按钮可以绘制矩形,从鼠标按下和鼠标释放中获取坐标。 现在,如果您删除按钮,代码将完美运行,这是代码。 //测试文件 package ActionTest; import java.awt.*; import javax.swing.*; public class MouseTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new MouseFrame(); frame.setTitle("MouseTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setSize(500,500); } }); } } 我的框架,调用鼠标组件 package ActionTest; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MouseFrame

2021-06-24 04:31:12    分类:技术分享    java   swing   jframe   jpanel   jbutton

在 JFrame 上的 JPanel 上绘制 Java [关闭](Java drawing on JPanel which on a JFrame [closed])

问题 很难说出这里问的是什么。 这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。 如需帮助澄清此问题以便重新打开它,请访问帮助中心。 7年前关闭。 嗨,我有一个 JFrame,上面有两个 JPanel。 我的意图是利用 JPanels。 任何人都可以分享任何Java代码吗? 回答1 所有 JComponents(其中 JPanel 继承自)都有一个您可以覆盖的paintComponent(Graphics g )方法。 基本上......哦......好吧,我认为这会更合适: http://java.sun.com/developer/technicalArticles/GUI/java2d/java2dpart1.html 天真样本: 源代码: import javax.swing.*; import java.awt.*; import java.awt.geom.*; public class X { public static void main( String [] args ) { JFrame frame = new JFrame(); frame.add( new JPanel() { public void paintComponent( Graphics g ) { super.paintComponent(g)

2021-06-24 03:39:47    分类:技术分享    java   swing   jpanel   custom-painting

Java return from a ShowOptionDialog from an inner JPanel

I used JOptionPane.showOptionDialog(null, new MyPanel(), "Import", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null); because I don't want the default buttons provided by the OptionDialog and I created my buttons inside the MyPanel extends JPanel So my problem is now how can I close that OptionDialog from inside the MyPanel fired by an ActionEvent? I don't care the return value, as long as that dialog disappears. And I realize this might not be the best design but I've been working on this for a lot of times so I'd prefer a fix that involves as little change in

2021-06-24 00:34:18    分类:问答    java   swing   jpanel   joptionpane   jdialog

JButton 在 JPanel 上,它不应该在 [关闭](JButton are on JPanel on which it isn't should be [closed])

问题 关闭。 这个问题需要调试细节。 它目前不接受答案。 想改善这个问题吗? 更新问题,使其成为 Stack Overflow 的主题。 5年前关闭。 改进这个问题 嗨,这是我的具体问题。 我试图用 for 循环向一个面板添加一个按钮。 这是用于创建 JButton 的 for 循环。 nizButtona=new JButton[22]; for(int i=0;i<nizButtona.length;i++){ nizButtona[i] = new JButton(); if(i==0){ nizButtona[i].setText("Započni kviz"); //Start quiz nizButtona[i].addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ cl.next(nizPanela[1]); } }); }else if(i==1){ nizButtona[i].setText("Izlaz"); //Quit nizButtona[i].addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e

2021-06-23 23:44:49    分类:技术分享    java   arrays   swing   jpanel   jbutton

Listening/Handling JPanel events

Good evening ladies and gentlemen, I have a problem with Java Swing that I cannot solve, maybe you can help me. Here it is: I have one JFrame which uses BorderLayout, and many JPanels. Every time I need to put up a new screen (i.e. from the Main Menu, when Search button is clicked, go to the Search Menu), I simply remove the component (JPanel) which is located in the center, and put the new screen (new JPanel) in the center instead. This way, I don't call all my header and footer objects every time I want to put up a new screen. Everything works fine with this system except this little problem

2021-06-23 21:08:03    分类:问答    java   swing   event-handling   jpanel   event-listener

这个JPanel代码好吗? 单击按钮时不显示其他 JPanel(Is this JPanel code good? Not showing the other JPanel when clicked on the button)

问题 当有人单击按钮时,如何让第二个 JPanel 显示出来? 代码给出了一个错误 - 来源未知。 我错过了什么? package Com.global; import java.awt.BorderLayout; public class Govinda extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private JPanel panel; private JPanel panel_1; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Govinda frame = new Govinda(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Govinda() { setDefaultCloseOperation

2021-06-23 16:58:05    分类:技术分享    java   swing   jpanel

按下按键时逐渐加速精灵; 松开按键时逐渐减速(Gradually accelerate sprite on key pressed; gradually decelerate on key released)

问题 我一直试图弄清楚如何在按下键时逐渐加速精灵,然后一旦释放键,逐渐减速到停止,就像小行星中的船一样。 如果可能的话,我想在没有任何游戏引擎的情况下做到这一点。 我搜索了所以这个时候,发现相关的问题,但他们并没有在我看来,正好回答我的问题。 到目前为止我想到的是: //while the key is being pressed //move by increasing y value //but continue to increase the amount y increases by as you hold this down, //until it reaches certain maxSpeed //when the key is released, gradually decelerate to zero 我只是不确定如何正确编程,因为我只能想办法增加相同的值而不是在保持时逐渐加速。 所以这是我的目标(逐渐加速然后逐渐减速) : 我是一个初学者,对所有想法持开放态度,但我可能没有正确解决这个问题的事实可能是因为我不像其他人那么了解 回答1 前面的问题几乎涵盖了这个主题,但如果你想要完美,还有一件事你需要注意:非均匀时间片,即不同持续时间的滴答声。 通常,您的 tick() 方法应该接受当前时间和最后一次滴答的持续时间作为参数。 (如果没有

2021-06-23 06:20:55    分类:技术分享    java   graphics   jpanel   sprite

JScrollPane doesn't work for my JPanel

first of all I must say that I have checked these questions and didn't find my answer : 1 , 2 , 3 , 4 , 5 , 6 , 7 and many other questions like so also I have checked these tutorials and examples: 1 , 9 , 10 , 11 and many other sites. but I couldn't fix my problem. and this is the simple kind of my code: public class Question extends JFrame { public Question() { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); setLayout(new BorderLayout()); setSize(d.width, d.height); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setPreferredSize(new

2021-06-23 04:18:02    分类:问答    java   swing   jframe   jpanel   jscrollpane

How can I display an image in a JPanel

My question here is how can I display an image into a JPanel? Other topics here asking something similar aren't clear to my about how can I do that. I have a directory in my project folder that have image files Project Folder/GUI/img, specifically gray.png and green.png which I want to display in a JPanel. I tried with the following code using ImageIcon and JLabel that I found in other post: ImageIcon image = new ImageIcon("GUI/img/gray.png"); JLabel label = new JLabel(image); //JPanel panel is already initialized by the IDE panel.add(label) But doesn't work... The JPanel remain empty without

2021-06-23 03:40:44    分类:问答    java   image   swing   jpanel