更改JLabel文本时奇怪的阴影

时间:2014-11-14 03:33:45

标签: java jlabel settext

我正在开发一个带有可点击按钮的小菜单程序,以及一个根据按钮点击而变化的图像。如果我单击一个按钮,我会在底部看到按钮的阴影,我会更改JLabel文本。我无法理解我的生活。任何建议将不胜感激。下面的视觉效果......谢谢

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

    private static void createAndShowGUI()
    {
        System.out.println("Created GUI on EDT? " +
                            SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Robert's VICI Prototype");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }
}

class MyPanel extends JPanel
{
    // Fields
    Image imageDisplayed;
    JLabel status;

    // Methods
    public MyPanel()
    {
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setLayout(null);

        JLabel title = new JLabel("CS380 TEAM 5 VICI Prototype");
        title.setFont(new Font("Tahoma", Font.BOLD, 20));
        title.setBounds(425, 10, 400, 40);
        add(title);

        status = new JLabel("Please click an option above.");
        status.setFont(new Font("Tahoma", Font.BOLD, 14));
        status.setBounds(425, 740, 400, 40);
        add(status);

        JButton choice1 = new JButton("Search Class");
        choice1.setBounds(50, 50, 150, 40);
        add(choice1);

        JButton choice2 = new JButton("Add Class");
        choice2.setBounds(225, 50, 150, 40);
        add(choice2);

        JButton choice3 = new JButton("Drop Class");
        choice3.setBounds(400, 50, 150, 40);
        add(choice3);

        JButton choice4 = new JButton("Verify Reg Hold");
        choice4.setBounds(575, 50, 150, 40);
        add(choice4);

        JButton choice5 = new JButton("Verify Reg Date");
        choice5.setBounds(750, 50, 150, 40);
        add(choice5);

        JButton choice6 = new JButton("Schedule Advisor");
        choice6.setBounds(925, 50, 150, 40);
        add(choice6);
        choice6.addActionListener(
            new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("Schedule Advisor button pressed.");
                    status.setText("Choose a date.");
                    imageDisplayed = new ImageIcon("C:\\Temp\\sa01.jpg").getImage();
                }
            });

        JButton exit = new JButton("EXIT");
        exit.setBounds(940, 750, 150, 40);
        add(exit);
        exit.addActionListener(
            new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }
            });

        imageDisplayed = new ImageIcon("C:\\Temp\\main.jpg").getImage();  

    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(1100, 800);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
    }


}

Visual Aid 1

Visual Aid 2

1 个答案:

答案 0 :(得分:0)

你已经破坏了油漆链......

@Override
public void paintComponent(Graphics g)
{
    g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}

你要称的第一件事是super.paintComponent(g)

@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}

Graphics上下文是共享资源,这意味着您获得的Graphics上下文也被用于绘制已绘制的其他组件。 paintComponent的一项工作是清除准备好要绘制的组件的Graphics上下文(填充背景)

有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting

你也应该避免使用null布局,像素完美布局是现代ui设计中的错觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的结束,您将花费越来越多的时间来纠正。有关详细信息,请参阅Why is it frowned upon to use a null layout in SWING?

在你全力防守屏幕布局之前,你不应该这样做。您应该将控件布置在单独的容器上(使用适当的布局管理器)并且您可以“绘制表面"应该是它自己的组成部分。有关详细信息,请参阅Laying Out Components Within a Container

如果您仍然没有被定罪,请查看Why setting setPreferredSize() on JFrame is bad?java expandable JDialog,了解Mac和Windows之间的差异示例