如何在GUI中移动图像 [英] How can I move the image in GUI

查看:123
本文介绍了如何在GUI中移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在此GUI中将图像设置为最右侧,如果您运行代码,您将看到图像位于单选按钮和复选框下,我想将图像向右移动,因此它不会去做。我该怎么做?

How can I set the image in this GUI to the far right, if you run the code you will see that the image goes under the radio buttons and checkboxes, I want to move the image to the right so It doesn't do that. How can I do that?

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class MyWizard {
    private JFrame frame = new JFrame("My Wizard");
    private JPanel panelContainer = new JPanel();
    private JPanel panelFirst = new JPanel();
    private JPanel panelSecond = new JPanel();
    private JPanel panelThird = new JPanel();

    //panels for buttons
    private JPanel panelButton = new JPanel();
    private JPanel panelButtonTwo = new JPanel();
    private JPanel panelButtonThree = new JPanel();

    private JButton btNext = new JButton ("Next");
    private JButton btNextTwo = new JButton ("Next");
    private JButton btNextThree = new JButton("Next");
    private JRadioButton btLdap, btKerbegos, btSpnego, btSaml2;
    private JCheckBox btSan, btNFS, btYUMserver;
    private CardLayout c1 = new CardLayout();
    private JScrollPane scrollPane;
    private JLabel lblPicture, lblPictureTwo;
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("FMW Components");


    public MyWizard() {
        //tree code
        final DefaultMutableTreeNode accessibility =
                add(root, "DB Tier", true);
        add(accessibility, "RAC", false);
        add(accessibility, "Gateways", false);
        add(accessibility, "Datavault", false);
        add(accessibility, "Agent", false);
        add(accessibility, "Custom Databases", false);
        root.add(accessibility);

        final DefaultMutableTreeNode browsing =
            new DefaultMutableTreeNode("APP Tier");
        add(browsing, "IDM (OID, OVD)", false);
        add(browsing, "IAM (Access Manager)", false);
        add(browsing, "BIEE", false);
        add(browsing, "Forms and Reports", false);
        add(browsing, "Discoverer", false);
        add(browsing, "Apps", false);
        add(browsing, "Apex(4.2.1)", false);
        root.add(browsing);

        final DefaultTreeModel treeModel = new DefaultTreeModel(root);
        final JTree tree = new JTree(treeModel);

        final CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
        tree.setCellRenderer(renderer);

        final CheckBoxNodeEditor editor = new CheckBoxNodeEditor(tree);
        tree.setCellEditor(editor);
        tree.setEditable(true);
        scrollPane = new JScrollPane(tree);
        //tree code ends

        panelFirst.setLayout(new BorderLayout()); 
        panelSecond.setLayout(new BorderLayout()); 
        panelThird.setLayout(new BorderLayout());

        panelContainer.setLayout(c1);

        panelButtonTwo.add(btNext);
        panelButtonThree.add(btNextTwo);
        panelButton.add(btNextThree);

        panelFirst.add(panelButtonTwo, BorderLayout.SOUTH);
        panelSecond.add(panelButtonThree, BorderLayout.SOUTH);
        panelThird.add(scrollPane, BorderLayout.CENTER);
        panelThird.add(panelButton,  BorderLayout.SOUTH);

        panelFirst.setBackground(Color.white);
        panelSecond.setBackground(Color.white);

        panelContainer.add(panelFirst, "1");
        panelContainer.add(panelSecond,"2");
        panelContainer.add(panelThird,"3");
        c1.show(panelContainer, "1");

        btNext.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                c1.show(panelContainer,"2");
            }
        });

        btNextTwo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                c1.show(panelContainer,"3");
            }
        });
        RadioButtons();
        Button();
        Buttons();
        CheckList();
        groupButton();
        Image();
        frame.add(panelContainer);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setSize(400,310);
        frame.setVisible(true); 
    }

    public void RadioButtons() {
        btLdap = new JRadioButton ("Ldap");
        btLdap.setBounds(60,85,100,20);
        btLdap.setBackground(Color.white);
        panelFirst.add(btLdap);

        btKerbegos = new JRadioButton ("Kerbegos");
        btKerbegos.setBounds(60,115,100,20);
        btKerbegos.setBackground(Color.white);
        panelFirst.add(btKerbegos);

        btSpnego =new JRadioButton("Spnego");
        btSpnego.setBounds(60,145,100,20);
        btSpnego.setBackground(Color.white);
        panelFirst.add(btSpnego);

        btSaml2 = new JRadioButton("Saml2");
        btSaml2.setBounds(60,175,100,20);
        btSaml2.setBackground(Color.white);
        panelFirst.add(btSaml2);
    }

    public void Button() {
        btNext.setBounds(250,240,100,20);
    }

    public void CheckList () {
        btSan = new JCheckBox ("San");
        btSan.setBounds(60,85,100,20);
        btSan.setBackground(Color.white);
        panelSecond.add(btSan);

        btNFS = new JCheckBox ("NFS");
        btNFS.setBounds(60,115,100,20);
        btNFS.setBackground(Color.white);
        panelSecond.add(btNFS);

        btYUMserver =new JCheckBox("Spnego");
        btYUMserver.setBounds(60,145,100,20);
        btYUMserver.setBackground(Color.white);
        panelSecond.add(btYUMserver);
    }

    public void Buttons() {
        btNextTwo.setBounds(250,240,100,20);
    }

    public void Image() {

        ImageIcon image = new ImageIcon("iconpic.png");
        lblPicture = new JLabel(image);
        panelFirst.add(lblPicture);

        ImageIcon imageTwo = new ImageIcon("iconpic.png");
        lblPictureTwo = new JLabel(imageTwo);
        panelSecond.add(lblPictureTwo);
    }

    private void groupButton() {

        ButtonGroup bg1 = new ButtonGroup( );

        bg1.add(btLdap);
        bg1.add(btKerbegos);
        bg1.add(btSpnego);
        bg1.add(btSaml2);
    }

    private static DefaultMutableTreeNode add(
        final DefaultMutableTreeNode parent, final String text,
        final boolean checked)
    {
        final CheckBoxNode data = new CheckBoxNode (text, checked);
        final DefaultMutableTreeNode node = new DefaultMutableTreeNode(data);
        parent.add(node);
        return node;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MyWizard();
            }
        });
    }
}


推荐答案

研究不同的布局经理。它们将有助于您尝试做什么。 http://docs.oracle.com/javase/tutorial/uiswing/layout /visual.html

Study the different layout managers. They will be helpful for what you are trying to do. http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

这篇关于如何在GUI中移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆