如何从左上角到右上角创建jlist?

时间:2014-03-07 05:24:16

标签: java swing layout jframe jlist

我正在使用swing创建一个jlist,我想从最右上角到最左上角创建该列表我该怎么做

我做了以下代码

package demoipis;


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

public class CreateList{
  public static void main(String[] args) {
  String subject[] = {"Math   English"};
  JFrame frame = new JFrame("Creating a JList Component");
  JPanel panel = new JPanel();
  Toolkit tk = Toolkit.getDefaultToolkit(); 
  int xSize = ((int) tk.getScreenSize().getWidth());  
int ySize = ((int) tk.getScreenSize().getHeight()); 
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JList list = new JList(subject);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  panel.add(list);
  frame.add(panel);
  frame.setSize(xSize,ySize);  
//frame.show();  

  frame.setVisible(true);
  }
}

我如何实现所需的输出?

提前致谢

2 个答案:

答案 0 :(得分:3)

JFrame的默认布局是BorderLayout。所以将JList添加到JScrollPane并将scrollpane添加到BorderLayout的NORTH。

JList list = new JLIst(...);
JScrollPane scrollPane = new JScrollPane(list);
frame.add(scrollPane, BorederLayout.NORTH);

现在列表将占据帧的所有水平空间。

答案 1 :(得分:0)

试试这个演示示例:

    import java.awt.*;
    import java.awt.Toolkit;
    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;

    class CreateList{
 public static void main(String[] args) {
 String subject[] = {"Math", " English", "  Hindhi", "   java", "  c ", " c++ "," cobol ","          python          ruby and rail                c#            .net         asp.net                           Haskell "};
JFrame frame = new JFrame("Creating a JList Component");
JPanel panel = new JPanel(new BorderLayout());
panel.setLayout(new BorderLayout());
Toolkit tk = Toolkit.getDefaultToolkit(); 
int xSize = ((int) tk.getScreenSize().getWidth());  
int ySize = ((int) tk.getScreenSize().getHeight()); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList list = new JList(subject);
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.setUndecorated(true);
panel.add(scrollPane, BorderLayout.NORTH);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

frame.add(panel);
frame.setSize(xSize,ySize);  
frame.show();  

frame.setVisible(true);
}
}

enter image description here