簡體   English   中英

如何制作一個看起來像一個簡單的股票市場應用程序附圖的GUI?

[英]How do I make a GUI that looks like the attached picture of a simple stock market app?

使用這些布局; FlowLayout,BoxLayout,GridLayout,BorderLayout和GridBagLayout我試圖使我的GUI看起來如下所示。 我試過Flow,Grid和Border,但我看不出它看起來一樣。

在此輸入圖像描述

我試圖模仿你想要的東西,似乎我更接近你想要的東西,看看代碼和圖像:

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

public class MakingLayout extends JFrame
{
    public MakingLayout()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());

        JPanel leftPanel = new JPanel();
        JLabel leftLabel = new JLabel("Output : ", JLabel.CENTER);

        leftPanel.add(leftLabel);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));

        JLabel centerFirstLabel = new JLabel("Google Inc.", JLabel.LEFT);
        JLabel centerSecondLabel = new JLabel("Price : 618.405", JLabel.LEFT);
        JLabel centerThirdLabel = new JLabel("<html>Change : <font color = red>-4.06 (-0.65%)</font> </html>", JLabel.LEFT);
        JLabel centerFourthLabel = new JLabel("Divident : 0.00", JLabel.LEFT);
        JLabel centerFifthLabel = new JLabel("Market Cap : 200.38", JLabel.LEFT);
        JLabel centerSixthLabel = new JLabel("Stock Exchange : NasdaqNM", JLabel.LEFT);

        centerPanel.add(centerFirstLabel);
        centerPanel.add(centerSecondLabel);
        centerPanel.add(centerThirdLabel);
        centerPanel.add(centerFourthLabel);
        centerPanel.add(centerFifthLabel);
        centerPanel.add(centerSixthLabel);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        gbc.weighty = 0.1;
        gbc.anchor = GridBagConstraints.PAGE_START;
        JLabel bottomLabel = new JLabel(" Input : ", JLabel.LEFT);

        JPanel digitPanel = new JPanel();
        JButton[] button = new JButton[10];
        for (int i = 0; i < 10; i++)
        {
            button[i] = new JButton("" + i);
            digitPanel.add(button[i]);
        }

        JPanel topLinePanel = new JPanel();
        JButton[] buttonTop = new JButton[11];
        buttonTop[0] = new JButton("Q");
        buttonTop[1] = new JButton("W");
        buttonTop[2] = new JButton("E");
        buttonTop[3] = new JButton("R");
        buttonTop[4] = new JButton("T");
        buttonTop[5] = new JButton("Y");
        buttonTop[6] = new JButton("U");
        buttonTop[7] = new JButton("I");
        buttonTop[8] = new JButton("O");
        buttonTop[9] = new JButton("P");
        buttonTop[10] = new JButton("DEL");
        for (int i = 0; i < 11; i++)
        {
            topLinePanel.add(buttonTop[i]);
        }

        JPanel middleLinePanel = new JPanel();
        JButton[] buttonMiddle = new JButton[10];
        buttonMiddle[0] = new JButton("A");
        buttonMiddle[1] = new JButton("S");
        buttonMiddle[2] = new JButton("D");
        buttonMiddle[3] = new JButton("F");
        buttonMiddle[4] = new JButton("G");
        buttonMiddle[5] = new JButton("H");
        buttonMiddle[6] = new JButton("J");
        buttonMiddle[7] = new JButton("K");
        buttonMiddle[8] = new JButton("L");
        buttonMiddle[9] = new JButton("RET");
        for (int i = 0; i < 10; i++)
        {
            middleLinePanel.add(buttonMiddle[i]);
        }

        JPanel bottomLinePanel = new JPanel();
        JButton[] buttonBottom = new JButton[8];
        buttonBottom[0] = new JButton("Z");
        buttonBottom[1] = new JButton("X");
        buttonBottom[2] = new JButton("C");
        buttonBottom[3] = new JButton("V");
        buttonBottom[4] = new JButton("B");
        buttonBottom[5] = new JButton("N");
        buttonBottom[6] = new JButton("M");
        buttonBottom[7] = new JButton(".");
        for (int i = 0; i < 8; i++)
        {
            bottomLinePanel.add(buttonBottom[i]);
        }

        bottomPanel.add(bottomLabel, gbc);
        gbc.gridy = 1;
        bottomPanel.add(digitPanel, gbc);
        gbc.gridy = 2;
        bottomPanel.add(topLinePanel, gbc);
        gbc.gridy = 3;
        bottomPanel.add(middleLinePanel, gbc);
        gbc.gridy = 4;
        bottomPanel.add(bottomLinePanel, gbc);

        contentPane.add(leftPanel, BorderLayout.WEST);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(bottomPanel, BorderLayout.PAGE_END);

        setContentPane(contentPane);
        pack();
        setVisible(true);       
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new MakingLayout();
            }
        });
    }
}

以下是代碼的結果:

makingLayout

使用Netbeans IDE表格編輯器 (它在網站上看起來很復古,現在真的很棒)。 我建議使用GridBagLayout。 我知道,我知道,這是令人討厭的代碼,但是使用Netbean的表單編輯器,當你掌握它時,它非常容易和強大。 以下是我在面板中設置組件的方法:

topPanel:
-leftPanel:
--outputLable
-rightPanel:
--companyLable
--priceLabel
--changeLabel
--dividentLabel
--marketCapLabel
--stockExchangeLabel
bottomPanel:
-panel1:
--inputLabel
-panel2:
--0Label
--1Label
--etc...
-panel3:
--qLabel
--wLabel
--etc...
-panel4:
--etc...
-panel5:
--etc...

並使按鈕的標簽具有您從圖像中分配的圖像圖標。 (如果你想為調整圖像大小的好方法,你可以看到我的方法在我的網站在這里 ,我不自稱是在這個最好的,但圖形用戶界面的東西,我對(一個很好的手段,如果你給我表單編輯器,哈哈)並且該方法可能不是最好的,但它對我很有用:)。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM