简体   繁体   English

用Java显示GUI组件

[英]Displaying GUI components in Java

I have a GUI window that I've created using Netbeans. 我有一个使用Netbeans创建的GUI窗口。 I then ported the code into my own program so that I can display .png's at my will. 然后,我将代码移植到自己的程序中,以便可以随意显示.png。

However, the GUI components are not displaying, and the window opens up with no size by default. 但是,GUI组件未显示,默认情况下该窗口打开时没有大小。

I need the window to initially open up with the GUI components visible, with the window of the correct size for everything to be visible. 我需要首先打开的窗口具有可见的GUI组件,并且具有正确大小的窗口以使所有内容都可见。

Can anyone help me out? 谁能帮我吗?

thanks 谢谢

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

public class AwtImage extends javax.swing.JFrame {

    private Image img;

    // Variables declaration - do not modify                     
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration            

    public static void main(String[] args){
        AwtImage ai = new AwtImage();
    }

    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jCheckBox2 = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Not Logged In");
        getContentPane().setLayout(null);

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(jTextField1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 540, 170, 22);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(0, 440, 166, 96);

        jCheckBox2.setText("Sit Out Next Hand");
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });
        getContentPane().add(jCheckBox2);
        jCheckBox2.setBounds(0, 410, 113, 23);

        pack();
    }// </editor-fold>


    private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                     
        // TODO add your handling code here:
    }               



    public AwtImage() {
        super("Image Frame");
        MediaTracker mt = new MediaTracker(this);
        img = Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\Robert\\Desktop\\ClientServer\\Poker Table Art\\TableAndChairs.png");
        mt.addImage(img,0);
        setSize(600,600);


        initComponents();
        setVisible(true);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we) {
                dispose();
            }
        });
    }

    public void update(Graphics g){
        paint(g);
    }

    public void paint(Graphics g) {
        if(img != null)
            g.drawImage(img, 0, 10, this);
       // else
        //    g.clearRect(0, 0, getSize().width, getSize().height);
    }
}

alt text http://img41.imageshack.us/img41/9795/openn.png alt text http://img709.imageshack.us/img709/5716/uponresize.png 替代文字http://img41.imageshack.us/img41/9795/openn.png 替代文字http://img709.imageshack.us/img709/5716/uponresize.png

without seeing your code, it's near impossible to help you however: 没有看到您的代码,几乎不可能为您提供帮助:

it sounds like you are missing a call to pack on your Window/JFrame which would cause the window to calculate it's size based on it's content 听起来您好像错过了打包在Window / JFrame上的调用,这将导致窗口根据其内容来计算其大小

and your UI components (checkbox + text boxes) are probably not showing up because you haven't added them correctly ie via the contents pane and a layout manager . 并且您的UI组件(复选框+文本框)可能未显示,因为您没有正确添加它们,即通过内容窗格和布局管理器

just a guess! 只是一个猜测!

There are several problems with the code. 代码有几个问题。

In addition to the other suggestions of using pack() or setting the frame size "after" adding the components to the frame and just before invoking the setVisible(true) method you need to look at the following. 除了使用pack()或在“将组件添加到框架中”之后以及在调用setVisible(true)方法之前“设置”框架大小的其他建议之外,您还需要查看以下内容。

The code is based on old AWT painting techniques, which should NOT be used with Swing. 该代码基于旧的AWT绘画技术,不应与Swing一起使用。 You should NEVER override the update() or paint() methods of the JFrame. 您永远不要重写JFrame的update()或paint()方法。 The reason the child components are not painted is that the paint() method is responsible for painting them but you overrode this default behaviour. 子组件未绘制的原因是paint()方法负责绘制它们,但是您覆盖了此默认行为。

When using Swing custom painting is done by overriding the paintComponent() method of a JComponent or JPanel, then you add this component to the content pane of the frame. 使用Swing时,通过覆盖JComponent或JPanel的paintComponent()方法来完成自定义绘制,然后将其添加到框架的内容窗格中。 Read the section from the Swing tutorial on Custom Painting for more information and working examples. 阅读有关定制绘画的Swing教程中的部分,以获取更多信息和工作示例。 One of the key points in the tutorial is to provide a preferred size for the component so it can be layed out properly by the layout manager. 本教程的重点之一是为组件提供首选的尺寸,以便布局管理器可以正确地对其进行布局。

You should NOT be using a KeyListener. 您不应该使用KeyListener。 Again, this is an old AWT technique. 同样,这是一种古老的AWT技术。 Swing now uses Key Bindings to map KeyStrokes to an Action. Swing现在使用键绑定将KeyStrokes映射到Action。 Read the section from the Swing tutorial on "How to Use Key Bindings" for more information. 阅读Swing教程中有关“如何使用键绑定”的部分,以获取更多信息。

You should NOT be using a WindowListener to close the frame again this is an old AWT technique. 您不应该再次使用WindowListener来关闭框架,这是一种古老的AWT技术。 Swing applications now use the frame.setDefaultCloseOperation(...) method to control this. 现在,Swing应用程序使用frame.setDefaultCloseOperation(...)方法进行控制。

The tutorial section on "How to Use Icons" shows you easier ways to load an image. 本教程的“如何使用图标”部分向您展示了加载图像的简便方法。

So overall I suggested you start by downloading the Swing tutorial to learn the Swing programming techniques. 因此总的来说,我建议您从下载Swing教程开始,以学习Swing编程技术。 The tutorial covers the basics and many simple examples to get you started. 本教程涵盖了基础知识和许多简单的示例,可以帮助您入门。

I persume you are calling ... 我想你在打...

.setSize(500,500);

on the main window? 在主窗口上?

if you know the size of the image, then I'd personally call... 如果您知道图片的大小,那么我个人会打电话给...

.setPreferedSize(x,x);
.setMinimenSize(x,x);
.setSize(x,x);
.pack();

on the main window/panel. 在主窗口/面板上。

can you post your code? 您可以发布代码吗?

setSize() should be moved after initComponents() setSize()应该移到initComponents()

But again, you wont need to port the code, you can edit the source code in Netbeans without difficulties, just keep initComponents folded. 但是同样,您将不需要移植代码,您可以轻松地在Netbeans中编辑源代码,只需保持initComponents折叠即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM