如何才能以某种方式获取GUI的内容? (JAVA)

时间:2017-02-28 00:58:39

标签: java swing user-interface layout layout-manager

所以,我是品牌spankin'新编程,所以提前感谢您的帮助。我正在尝试将这个基础2放到基础10 /基础10到我已经制作成GUI的基础2计算器。对于我的生活,我无法弄清楚如何很好地格式化它。我试图让它看起来像下面这样:顶部有两个单选按钮,输入文本字段低于那些,转换按钮低于那个,输出字段低于那个,清除按钮低于那个。有关如何实现这一目标的任何想法?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Confirm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //int salesPrice, discountAmount, totalPrice;
        UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
        if (Session["salesprice"] != null && Session["discountamount"] != null && Session["totalprice"] != null)
        {
            lblSalesPrice.Text = Session["salesprice"].ToString();

            lblDiscountAmount.Text = Session["discountamount"].ToString();
            lblTotalPrice.Text = Session["totalprice"].ToString();

            /*
            decimal salesPrice = Convert.ToDecimal(txtSalesPrice.Text);
            decimal discountPercent = Convert.ToDecimal(txtDiscountPercent.Text) / 100;

            decimal discountAmount = salesPrice * discountPercent;
            decimal totalPrice = salesPrice - discountAmount;

            lblDiscountAmount.Text = discountAmount.ToString("c");
            lblTotalPrice.Text = totalPrice.ToString("c");*/

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "This function hasn't been implemented yet.";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Server.Transfer("Default.aspx");
    }
}

1 个答案:

答案 0 :(得分:0)

对于简单的布局,您可以将GridLayout与一列一起使用,然后使用一组带有FlowLayout的子面板,这些子面板根据单行中的可用空间对齐组件。如果你想要更多控制,我建议你学习一下GridBayLayout管理器,这是一个更灵活的GridLayout版本。

public class ExampleGUI {

    public ExampleGUI() {
        init();
    }

    private void init() {
        JFrame frame = new JFrame();

        // Set the frame's layout to a GridLayout with one column
        frame.setLayout(new GridLayout(0, 1));
        frame.setPreferredSize(new Dimension(300, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Child panels, each with FlowLayout(), which aligns the components
        // in a single row, until there's no more space
        JPanel radioButtonPanel = new JPanel(new FlowLayout());
        JRadioButton button1 = new JRadioButton("Option 1");
        JRadioButton button2 = new JRadioButton("Option 2");
        radioButtonPanel.add(button1);
        radioButtonPanel.add(button2);

        JPanel inputPanel = new JPanel(new FlowLayout());
        JLabel inputLabel = new JLabel("Input: ");
        JTextField textField1 = new JTextField(15);
        inputPanel.add(inputLabel);
        inputPanel.add(textField1);

        JPanel convertPanel = new JPanel(new FlowLayout());
        JButton convertButton = new JButton("Convert");
        convertPanel.add(convertButton);

        JPanel outputPanel = new JPanel(new FlowLayout());
        JLabel outputLabel = new JLabel("Output: ");
        JTextField textField2 = new JTextField(15);
        outputPanel.add(outputLabel);
        outputPanel.add(textField2);

        // Add the child panels to the frame, in order, which all get placed
        // in a single column
        frame.add(radioButtonPanel);
        frame.add(inputPanel);
        frame.add(convertPanel);
        frame.add(outputPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        ExampleGUI example = new ExampleGUI();
    }

}

最终结果:

enter image description here