2024/11/09
今天绘制软件设计实验一的GUI页面
package com.mes; import com.mes.util.TransConfig; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; public class InitForm { private JFrame frame; private JTextArea textArea; private JComboBox<String> comboBox; private JButton button; private JLabel resultLabel; public InitForm() { // 创建窗体 frame = new JFrame("百度翻译"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); // 设置窗体布局为边界布局,方便对各组件进行布局安排 frame.setLayout(new BorderLayout()); JPanel inputPanel = new JPanel(new FlowLayout()); // 创建文本域 textArea = new JTextArea(5, 20); textArea.setFont(new Font("宋体", Font.PLAIN, 16)); JScrollPane scrollPane = new JScrollPane(textArea); inputPanel.add(scrollPane); // 创建下拉框 comboBox = new JComboBox<>(new String[]{"英转中", "中转英", "自动识别转中", "自动识别转英"}); comboBox.setFont(new Font("宋体", Font.PLAIN, 16)); inputPanel.add(comboBox); // 设置"中转英"为默认选中项 comboBox.setSelectedItem("中转英"); frame.add(inputPanel, BorderLayout.NORTH); // 创建面板用于放置按钮 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); // 创建按钮 button = new JButton("翻译"); button.setFont(new Font("宋体", Font.BOLD, 18)); button.setPreferredSize(new Dimension(100, 40)); buttonPanel.add(button); // 将按钮面板添加到窗体的中部区域 frame.add(buttonPanel, BorderLayout.CENTER); // 创建用于显示结果的文本标签 resultLabel = new JLabel(); resultLabel.setFont(new Font("宋体", Font.PLAIN, 16)); resultLabel.setHorizontalAlignment(SwingConstants.CENTER); resultLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 设置标签的边距 JPanel resultPanel = new JPanel(new BorderLayout()); resultPanel.add(resultLabel, BorderLayout.CENTER); resultPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 50, 10)); // 设置底部间距为 50 resultPanel.add(resultLabel, BorderLayout.CENTER); frame.add(resultPanel, BorderLayout.SOUTH); // 为下拉框添加事件监听器 comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (comboBox.getSelectedItem().equals("英转中")) { TransConfig.getInstance().setFrom("en"); TransConfig.getInstance().setTo("zh"); } else if (comboBox.getSelectedItem().equals("中转英")) { TransConfig.getInstance().setFrom("zh"); TransConfig.getInstance().setTo("en"); } else if (comboBox.getSelectedItem().equals("自动识别转中")) { TransConfig.getInstance().setFrom("auto"); TransConfig.getInstance().setTo("zh"); } else if (comboBox.getSelectedItem().equals("自动识别转英")) { TransConfig.getInstance().setFrom("auto"); TransConfig.getInstance().setTo("en"); } } }); // 为按钮添加事件监听器 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String query = textArea.getText(); try { resultLabel.setText("翻译结果: " + TransConfig.getTransBySDK(query)); } catch (IOException ex) { throw new RuntimeException(ex); } } }); // 设置窗体可见 frame.setVisible(true); } }
主函数调用:
package com.mes; import com.mes.util.TransApi; import com.mes.util.TransConfig; import java.io.IOException; /** * Hello world! * */ public class App { public static void main(String[] args) throws IOException { new InitForm(); // TransConfig transConfig = TransConfig.getInstance(); // transConfig.setFrom("en"); // transConfig.setTo("zh"); // System.out.println(TransConfig.getTransBySDK("hello world")); // System.out.println(transConfig.getTrans("hello world")); // // transConfig.setFrom("zh"); // transConfig.setTo("en"); // System.out.println(TransConfig.getTransBySDK("向天再借五百年")); // System.out.println(transConfig.getTrans("向天再借五百年")); } }

浙公网安备 33010602011771号