| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1082 人关注过本帖
标题:java中列表框的内容选择对应的选项,后对应的文本框要显示对应的该选项的文本 ...
只看楼主 加入收藏
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:15 
java中列表框的内容选择对应的选项,后对应的文本框要显示对应的该选项的文本?
JTextFiekd wbk1=new JTextFidle();
JList lb1=new JList();
lb1.addListSelectionListener(new ListSelectionListener(){
                        public void valueChanged(ListSelectionEvent e){
                            wbk1.setText(lb1.getSelectedIndex());
                        }
                    });
文本框没有显示文本,我知道方法不对,在API找半天没找着,请问用什么文本框用什么方法来解释lb1.getSelectedIndex()??
搜索更多相关主题的帖子: public 文本框 java 
2013-12-09 21:08
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
收藏
得分:0 
求大神搭救,明天交作业现在卡在这里做不下去了啊啊啊!!!
2013-12-09 22:26
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:0 
JTextFidle类的代码贴出来看看

学海无涯#¥%……&*(
2013-12-10 12:51
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
收藏
得分:0 
package test1;

import java.awt.*;
import java.awt.event.ActionEvent;//事件类
import java.awt.event.ActionListener;//监听类
import java.awt.event.InputEvent;//输入事件
import java.awt.event.KeyAdapter;//接收键盘事件的抽象适配器类
import java.awt.event.KeyEvent;//表示组件中发生键击的事件
import java.awt.event.MouseAdapter;//接收鼠标事件的抽象适配器类
import java.awt.event.MouseEvent;//指示组件中发生鼠标动作的事件
import java.awt.event.WindowAdapter;//接收窗口事件的抽象适配器类
import java.awt.event.WindowEvent;//指示窗口状态改变的低级别事件
import 从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
import 将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
import 文件和目录路径名的抽象表示形式
import 用来读取字符文件的便捷类
import 用来写入字符文件的便捷类
import 当发生某种 I/O 异常时,抛出此异常。此类是失败或中断的 I/O 操作生成的异常的通用类。

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Test15 extends JFrame {
    private JMenuItem menuOpen;
    private JMenuItem menuSave;
    private JMenuItem menuSaveAs;
    private JMenuItem menuClose;

    private JMenu editMenu;
    private JMenuItem menuCut;
    private JMenuItem menuCopy;
    private JMenuItem menuPaste;

    private JMenuItem menuAbout;
    private JMenuItem menuFont;
    private JTextArea textArea;
    private JLabel stateBar;
    private JFileChooser fileChooser;

    private JPopupMenu popUpMenu;

    public Test15() {
        super("新建文本文件");
        setUpUIComponent();
        setUpEventListener();
        setVisible(true);
    }

    private void setUpUIComponent() {
        setSize(640, 480);

        // 菜单栏
        JMenuBar menuBar = new JMenuBar();

        // 设置「文件」菜单
        JMenu fileMenu = new JMenu("文件");
        menuOpen = new JMenuItem("打开");
        // 快捷键设置
        menuOpen.setAccelerator(//public void setAccelerator(KeyStroke keyStroke),keyStroke - 键击组合
                KeyStroke.getKeyStroke(//public static KeyStroke getKeyStroke(int keyCode,int modifiers),返回的 KeyStroke 将对应于一个按键操作。
                        KeyEvent.VK_O,
                        InputEvent.CTRL_MASK));
        menuSave = new JMenuItem("保存");
        menuSave.setAccelerator(
                KeyStroke.getKeyStroke(
                        KeyEvent.VK_S,
                        InputEvent.CTRL_MASK));
        menuSaveAs = new JMenuItem("另存为");

        menuClose = new JMenuItem("关闭");
        menuClose.setAccelerator(
                KeyStroke.getKeyStroke(
                        KeyEvent.VK_Q,
                        InputEvent.CTRL_MASK));

        fileMenu.add(menuOpen);
        fileMenu.addSeparator(); // 分隔线
        fileMenu.add(menuSave);
        fileMenu.add(menuSaveAs);        
        fileMenu.addSeparator(); // 分隔线
        fileMenu.add(menuClose);

        // 设置「编辑」菜单        
        JMenu editMenu = new JMenu("编辑");
        menuCut = new JMenuItem("剪切");
        menuCut.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_X,
                        InputEvent.CTRL_MASK));
        menuCopy = new JMenuItem("复制");
        menuCopy.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_C,
                        InputEvent.CTRL_MASK));
        menuPaste = new JMenuItem("粘贴");
        menuPaste.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_V,
                        InputEvent.CTRL_MASK));
        editMenu.add(menuCut);
        editMenu.add(menuCopy);
        editMenu.add(menuPaste);

        // 设置「关于」菜单        
        JMenu aboutMenu = new JMenu("关于");
        menuAbout = new JMenuItem("关于JNotePad");
        aboutMenu.add(menuAbout);


        //设置[格式]菜单
        JMenu formatMenu=new JMenu("格式");
        menuFont=new JMenuItem("字体");
        formatMenu.add(menuFont);

        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(formatMenu);
        menuBar.add(aboutMenu);



        setJMenuBar(menuBar);

        // 文字编辑区域
        textArea = new JTextArea();
        textArea.setFont(new Font("宋体", Font.PLAIN, 16));
        textArea.setLineWrap(true);//设置文本区的换行策略。
        JScrollPane panel = new JScrollPane(textArea,//public JScrollPane(Component view,int vsbPolicy,int hsbPolicy),view - 将显示在滚动窗格视口中的组件,vsbPolicy - 指定垂直滚动条策略的一个整数,hsbPolicy - 指定水平滚动条策略的一个整数

                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,//设置垂直滚动条在需要时显示
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);//设置水平滚动条始终不显示

        Container contentPane = getContentPane();
        contentPane.add(panel, BorderLayout.CENTER);  

        // 状态栏
        stateBar = new JLabel("未修改");
        stateBar.setHorizontalAlignment(SwingConstants.LEFT); //设置标签内容沿 X 轴的对齐方式。
        stateBar.setBorder(
                BorderFactory.createEtchedBorder());
        contentPane.add(stateBar, BorderLayout.SOUTH);      

        popUpMenu = editMenu.getPopupMenu();
        fileChooser = new JFileChooser();
    }

    private void setUpEventListener() {
        // 按下窗口关闭钮事件处理
        addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        closeFile();
                        
                    }
                }
                );

        // 菜单 - 打开
        menuOpen.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        openFile();
                    }
                }
                );

        // 菜单 - 保存
        menuSave.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        saveFile();
                    }
                }
                );

        // 菜单 - 另存为
        menuSaveAs.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        saveFileAs();
                    }
                }
                );

        // 菜单 - 关闭文件
        menuClose.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        closeFile();
                    }
                }
                );

        // 菜单 - 剪切
        menuCut.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        cut();
                    }
                }
                );

        // 菜单 - 复制
        menuCopy.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        copy();
                    }
                }
                );

        // 菜单 - 粘贴
        menuPaste.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        paste();
                    }
                }
                );
        // 格式 - 字体
        menuFont.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                new ChildWindos();   
            }
            class ChildWindos extends JFrame{
                JLabel bq1,bq2,bq3;
                JTextField wbk1,wbk2,wbk3;
                JList lb1,lb2,lb3;
                JButton an1,an2;
                public ChildWindos(){
                    super("字体");
                    setUpUIComponent_childPane();
                    this.setResizable(false);
                    this.setVisible(true);
                    this.setSize(500,500);
                }
                private void setUpUIComponent_childPane(){
                    bq1=new JLabel("字体(F)");
                    bq2=new JLabel("字形(Y)");
                    bq3=new JLabel("大小(S)");
                    wbk1=new JTextField();
                    wbk2=new JTextField();
                    wbk3=new JTextField();
                    an1=new JButton("确认");
                    an2=new JButton("取消");
                    //设置字体
                    String[] zt={"宋体","楷体","微软雅黑","新宋体","幼圆","隶书"};
                    lb1=new JList(zt);
                    JScrollPane gd1=new JScrollPane(lb1);//滚动组
                    lb1.setVisibleRowCount(3);

                    //设置样式
                    String[] zx={"常规","倾斜","粗体"};
                    lb2=new JList(zx);
                    JScrollPane gd2=new JScrollPane(lb2);//滚动组
                    lb2.setVisibleRowCount(3);

                    //设置磅值大小
                    String[] dx={"8","9","10","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
                    lb3=new JList(dx);
                    JScrollPane gd3=new JScrollPane(lb3);//滚动组
                    lb3.setVisibleRowCount(3);


                    this.setLayout(null);
                    bq1.setBounds(10,10,50,20);
                    wbk1.setBounds(10,30,150,20);
                    gd1.setBounds(10,50,150,200);
                    bq2.setBounds(200,10,50,20);
                    wbk2.setBounds(200,30,150,20);
                    gd2.setBounds(200,50,150,200);
                    wbk3.setBounds(380,30,110,20);
                    bq3.setBounds(380,10,50,20);
                    gd3.setBounds(380,50,110,200);
                    an1.setBounds(330,430,70,30);
                    an2.setBounds(410,430,70,30);
                    Container contentPane=this.getContentPane();
                    contentPane.add(wbk1);
                    contentPane.add(wbk2);
                    contentPane.add(wbk3);
                    contentPane.add(bq1);
                    contentPane.add(bq2);
                    contentPane.add(bq3);
                    contentPane.add(gd1);
                    contentPane.add(gd2);
                    contentPane.add(gd3);
                    contentPane.add(an1);
                    contentPane.add(an2);
                }
                private void setUpEventListener_childPane(){
                    //字体事件
                    lb1.addListSelectionListener(new ListSelectionListener(){
                        public void valueChanged(ListSelectionEvent e){
                            wbk1.setText(String.valueOf(lb1.getSelectedIndex()));
                           
                        }
                    });

                    lb2.addListSelectionListener(new ListSelectionListener(){
                        public void valueChanged(ListSelectionEvent e) {
                           
                           
                        }
                    });
                    lb3.addListSelectionListener(new ListSelectionListener(){
                        public void valueChanged(ListSelectionEvent e) {
                           
                           
                        }   
                    });
                    //窗口事件
                    this.addWindowListener(
                            new WindowAdapter() {
                                public void windowClosing(WindowEvent e) {
                                    dispose();
                                }
                            }
                            );
                    //按钮时间
                    an1.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) {
                            dispose();
                        }
                    });
                    an2.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent e) {
                           
                           
                        }
                    });
                }


            }

        });
        // 菜单 - 关于
        menuAbout.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        // 显示对话框
                        JOptionPane.showOptionDialog(null,
                                "程序名称:\n    JNotePad \n" +
                                        "程序设计:\n      \n" +
                                        "简介:\n    一个简单的文字编辑器\n" +
                                        "    可作为验收Java的实现对象\n" +
                                        "    欢迎网友下载研究交流\n\n" +
                                        " /",
                                        "关于JNotePad",
                                        JOptionPane.DEFAULT_OPTION,
                                        JOptionPane.INFORMATION_MESSAGE,
                                        null, null, null);
                    }
                }
                );      

        // 编辑区键盘事件
        textArea.addKeyListener(
                new KeyAdapter() {
                    public void keyTyped(KeyEvent e) {
                        processTextArea();
                    }
                }
                );

        // 编辑区鼠标事件
        textArea.addMouseListener(
                new MouseAdapter() {
                    public void mouseReleased(MouseEvent e) {
                        if(e.getButton() == MouseEvent.BUTTON3)
                            popUpMenu.show(editMenu, e.getX(), e.getY());
                    }

                    public void mouseClicked(MouseEvent e) {
                        if(e.getButton() == MouseEvent.BUTTON1)
                            popUpMenu.setVisible(false);
                    }
                }
                );        
    }

    private void openFile() {
        if(isCurrentFileSaved()) { // 文件是否为保存状态
            open(); // 打开
        }
        else {
            // 显示对话框
            int option = JOptionPane.showConfirmDialog(
                    null, "文件已修改,是否保存?",
                    "保存文件?", JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE, null);
            switch(option) {
            // 确认文件保存
            case JOptionPane.YES_OPTION:
                saveFile(); // 保存文件
                break;
                // 放弃文件保存
            case JOptionPane.NO_OPTION:
                open();
                break;
            }
        }
    }
    private boolean isCurrentFileSaved() {
        if(stateBar.getText().equals("未修改")) {
            return false;
        }
        else {
            return true;
        }
    }
    private void open() {
        // fileChooser 是 JFileChooser 的实例
        // 显示文件选取的对话框
        int option = fileChooser.showOpenDialog(null);

        // 使用者按下确认键
        if(option == JFileChooser.APPROVE_OPTION) {
            try {
                // 开启选取的文件
                BufferedReader buf =
                        new BufferedReader(
                                new FileReader(
                                        fileChooser.getSelectedFile()));

                // 设定文件标题
                setTitle(fileChooser.getSelectedFile().toString());
                // 清除前一次文件
                textArea.setText("");
                // 设定状态栏
                stateBar.setText("未修改");
                // 取得系统相依的换行字符
                String lineSeparator = System.getProperty("line.separator");
                // 读取文件并附加至文字编辑区
                String text;
                while((text = buf.readLine()) != null) {
                    textArea.append(text);
                    textArea.append(lineSeparator);
                }

                buf.close();
            }   
            catch(IOException e) {
                JOptionPane.showMessageDialog(null, e.toString(),
                        "开启文件失败", JOptionPane.ERROR_MESSAGE);
            }
        }        
    }
    private void saveFile() {
        // 从标题栏取得文件名称
        File file = new File(getTitle());

        // 若指定的文件不存在
        if(!file.exists()) {
            // 执行另存为
            saveFileAs();
        }
        else {
            try {
                // 开启指定的文件
                BufferedWriter buf =
                        new BufferedWriter(
                                new FileWriter(file));
                // 将文字编辑区的文字写入文件
                buf.write(textArea.getText());
                buf.close();
                // 设定状态栏为未修改
                stateBar.setText("未修改");
            }
            catch(IOException e) {
                JOptionPane.showMessageDialog(null, e.toString(),
                        "写入文件失败", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void saveFileAs() {
        // 显示文件对话框
        int option = fileChooser.showSaveDialog(null);

        // 如果确认选取文件
        if(option == JFileChooser.APPROVE_OPTION) {
            // 取得选择的文件
            File file = fileChooser.getSelectedFile();

            // 在标题栏上设定文件名称
            setTitle(file.toString());

            try {
                // 建立文件
                file.createNewFile();
                // 进行文件保存
                saveFile();
            }
            catch(IOException e) {
                JOptionPane.showMessageDialog(null, e.toString(),
                        "无法建立新文件", JOptionPane.ERROR_MESSAGE);
            }
        }   
    }

    private void closeFile() {
        // 是否已保存文件
        if(isCurrentFileSaved()) {
            // 释放窗口资源,而后关闭程序
            System.exit(0);
        }
        else {
            int option = JOptionPane.showConfirmDialog(
                    null, "文件已修改,是否保存?",
                    "保存文件?", JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE, null);

            switch(option) {
            case JOptionPane.YES_OPTION:
                saveFile();
                break;
            case JOptionPane.NO_OPTION:
                System.exit(0);
            }
        }
    }

    private void cut() {
        textArea.cut();
        stateBar.setText("已修改");
        popUpMenu.setVisible(false);
    }

    private void copy() {
        textArea.copy();
        popUpMenu.setVisible(false);   
    }

    private void paste() {
        textArea.paste();
        stateBar.setText("已修改");
        popUpMenu.setVisible(false);
    }

    private void processTextArea() {
        stateBar.setText("已修改");
    }

    public static void main(String[] args) {
        new Test15();
    }   
}
2013-12-10 17:20
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
收藏
得分:0 
求大神们围观啊啊啊
2013-12-10 17:22
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
收藏
得分:0 
大神都哪里去了呢呢,求关注啊啊啊
2013-12-11 00:29
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:0 
神,用代码标签啊,这看的

学海无涯#¥%……&*(
2013-12-11 09:52
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:20 
运行了下,程序正常,你说的是什么问题,标红的代码怎么了?

学海无涯#¥%……&*(
2013-12-11 09:58
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
收藏
得分:0 
在字体里面怎么通过选择列表的选项然后可以在上面的文本框显示选项的文本
2013-12-11 13:11
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:0 
回复 9楼 暂不存在
不懂你的意思,看到右击鼠标的坐标不对。你看看那里是不是有问题

学海无涯#¥%……&*(
2013-12-11 13:26
快速回复:java中列表框的内容选择对应的选项,后对应的文本框要显示对应的该选项 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042643 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved