当前位置:网站首页>Summary of JFrame knowledge points 2
Summary of JFrame knowledge points 2
2022-07-01 11:45:00 【dengfengling999】
- Add border
The first way to write it :
Border border=new LineBorder(Color.BLUE,4);// Linear border
a1.setBorder(border1);
among ,Border It's the interface ,LineBorder Is the concrete implementation class
Another way of writing :
Border border2=BorderFactory.createLineBorder(Color.RED,4);
a2.setBorder(border2);
among ,BorderFactory It encapsulates many tools and methods (static), Used to create format borders
- Border style :
// Linear border
Border border1=BorderFactory.createLineBorder(Color.RED,4);
a1.setBorder(border1);
// Dotted box
a1.setBorder(BorderFactory.createDashedBorder(Color.RED,4,2));
// Separate borders , Specify the width of each box
Border border2=BorderFactory.createMatteBorder(0,0,4,0,Color.DARK_GRAY);
a2.setBorder(border2);
//3D Frame
Border border3=BorderFactory.createRaisedBevelBorder();
a3.setBorder(border3);
Border border4=BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
a3.setBorder(border4);
// Combine borders
Border bbb=BorderFactory.createCompoundBorder(border1,border2);//border1 Outside ,border2 Inside
a4.setBorder(bbb);
- Borders and margins :
- Blank border :
Blank border EmptyBorder, It is usually set in the sentence on the inner wall
for example :
root.setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
- Dialog box :
JOptionPane, With message prompt box
-JOptionPane.showMessageDialog(), Message box
- JOptionPane.showConfirmDialog(), Message confirmation box
- JOptionPane.showInputDialog(), Input prompt box
among ,show*() All tools and methods , Static method (static)
- File selection dialog :
JFileChooser The file selection box is used to select a file / Catalog
JFileChoose Use :
1 Show dialog showOpenDialog()/showSaveDialog()
2 Determine the return value
APPROVE_OPTION, The user confirmed the selection
CANCEL_OPTION, The operation was cancelled by the user
3 Get the file path
File file=chooser.getSelectedFile()
-showOpenDialog()
When you need to open a file, call
-showSaveDialog()
When you need to save a file, call
-showOpenDialog()/DIRECTORIES_ONLY
When you need to choose one
- Custom dialog :
JDialog, Represents a dialog window
Class SimpleDialog extends JDialog{
}
and JFrame Usage is similar. , Make interface layout in the construction method
JDialog At tectonic time , Need one owner Belong to , That is, from which window he jumped out
public SimpleDialog (Window owner){
super(owner);
}
among ,Window yes JFrame、JDialog
Show dialog :
SimpleDialog dlg=new SimpleDialog(this);
dig.setTitle(“ Tips ”);
dlg.setModal(true);// Modal ( Blocking mode )
dlg.setSize(200,100);
dlg.setVisible(true);
Modal Modal , That is, display in a blocked way :
1 The interface is blocked , When the dialog box is displayed , The back window owner Lose focus
2 Programmatically blocked ,dlg.setVisible() Has been running , Until the dialog box is closed
- Get user input :
- In the dialog , Add a text box
JTextField userInput=..
- When the dialog box closes , Get user input
public String getValue(){
}
- Confirm or cancel :
1 add to “ determine ”、“ Cancel ” Button
2 Add a field
public boolean accepted=false;
3 Event handling , Click the OK button ,
this.accepted=true;
this.setVisible(false);
4 Judge the result of the dialog , Judge the user's operation It's to be sure 、 Or cancel ?
If(dlg.accepted){
}
- Dialog box display location :
-centerOwner() Display in the original central position
-centerInScreen() Displayed in the center of the screen
Be careful , This line should be placed in setVisible() Before ,setSize() after
11.Component
see JFrame Inheritance tree of , Window class
JFrameàWindowàComponent
see JDialog
JDialogàWindowàComponent
see JButton Inheritance tree of Control class
JButtonàJComponentàComponent
- Get the window where a control is located , The following two methods can be used :
Window win =SwingUtilities.windowForComponent(comp);
perhaps
Window win =SwingUtilities.getWindowAncestor(comp);
among ,comp Represents a control
- Right-click menu
JPopupMenu Right-click menu , Or context menu
1 Respond to the right mouse button click
2 Pop up menu
- establish JPopupMenu menu
- add to JMenultem A menu item
- Pop up menu window popup.show()
13. Menu item processing : When a menu item is selected , Execute response processing
JMenuItem
item.addActionListener(listener)// Add listener
item.setActionCommand(“save as”)// Add command code
- List box
JList The list box control displays a list of data items
1 Create list box
JList<T> listox=new JList<>();
and JComboBox similar ,T Is the type of data item
2 Add scroll bar
JScrollPane scrollPane=new JScrollPane(listbox);
Is actually the JList The control package is placed in JScrollPane in , Support scrolling
3 Add window
root.add(scrollPane,BorderLayout.CENTER);
4 Set up the data
DefaultListModel<String> model=new DefaultListModel();
Model.addElement(“ Holding pressure ”);
…
listbox.setModel(model);
In programming ,Model Generally, it means data
- Single choice
JList There is a single choice mode , And multi choice mode
Radio mode :
listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
List item operation :
- Add a
model.addElement(value)
model.add(index,value)
- To delete a
index=listbox.getSelectedIndex() , Index selected by the user Unselected send back -1
value=listbox.getSelectedValue() Which item the user selected
model.remove(index) Delete
Multiple choice mode :
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
Press Crtl Select multiple intervals , Press Shift Multiple choices in a row
Get the selected index :
int indexArray=listbox.getSelectedIndices();
perhaps
List <String> values=listbox.getSelectedValueList()
When multiple items are deleted , It should be deleted from the back to the front
Int[] indexArray=listbox.getSelectedIndices();
for(int i=indexArray.length-1;i>=0;i--){
int index=indexArray[i];
model.remove(index);
}
- List box mouse click event processing
default ,JList A mouse event listener has been added inside , When you click the left mouse button , Perform the selected operation
Left click processing :
step :
1 Delete the original mouse listener
Listbox.removeMouseListener()
Listbox.removeMouseMotionListener()
2 Add your own mouse listener
Listbox.addMouseListener()
And determine whether the mouse is BUTTON1
relevant API:
-listbox.clearSelection() Clear options
-listbox.setSelectedIndex(index) Choose one
-listbox.addSelectionInterval(from,to) Add selection ( Multiple choice mode )
-index=listbox.locationToIndex(point) Calculate the position of the point
-bounds=listbox.getCellBounds(index,index) Calculate the position rectangle of an item
Right click to process :
default ,List The list box does not handle right clicking
direct addMouseListener() Add right-click processing .
- Type of list item
JLis<T>listbox=new JList();
Data item T It can be of any kind , It can be String、Integer、Student…
And JComboBox<T> similar
- Display of list box :
Interface ListCellRenderer, Responsible for the display of list items , Including Fonts 、 Color 、 Row height 、 background 、 Selected state
step :
1 Implement a class Renderer
Public class MyListCellRenderer implements ListCellRenderer{}
2 Use this Renderer
Listbox.setCellRenderer(new MyListCellRenderer())
Next , Each list item consists of this Renderer Responsible for the drawing
Selected state :boolean isSelected
If(isSelected){
..
}else{
..
}
- Table control :
JTable Table control , Used to display tabular data and JList Use similar
TableCellRenderer, Custom cell display ,
for example , The display of gender column :
tableColumn column2=table.getColumnModel().getColumn(2); column2.setCellRenderer(new TableRenderer_Sex());
- Add and delete
Add a row , tableMode.addRow(vector)
Delete a line , tableMode.removeRow(index)
Get the selected line number , int [] rows=table.getSelectedRows()
Change the value of the cell , tableModel.setValueAt(new Value,row,column)
- default , Double click a cell in the table to edit
To disable editing ,, Should be rewritten isCellEditable() Method
establish MyTable Class rewriting isCellEditable() Order return false
- Add right-click menu support to the table
22. Add dialog :EditDialog
边栏推荐
- Numpy的矩阵
- Binary stack (I) - principle and C implementation
- Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake
- Unittest 框架介绍及第一个demo
- 开发说,“ 这个不用测,回归正常流程就行 “,测试人员怎么办?
- S7-1500plc simulation
- Nordic nrf52832 flash 下载M4错误
- 对于mvvm和mvc的理解
- Activity workflow engine
- 证券账户随便哪里开都能使用吗 开户安全吗
猜你喜欢

Mechanism and type of CPU context switch

How to set decimal places in CAD

CAD如何设置标注小数位

S7-1500PLC仿真

Exposure: a white box photo post processing framework reading notes

Neo4j 中文开发者月刊 - 202206期

Several cases of index failure

About keil compiler, "file has been changed outside the editor, reload?" Solutions for

使用set_handler过滤掉特定的SystemC Wraning &Error Message

Exploration and practice of inress in kubernetes
随机推荐
Introduction to unittest framework and the first demo
Adjacency matrix undirected graph (I) - basic concepts and C language
Mysql的四个隔离级别是如何实现的 (简要)
Matrix of numpy
Continuous delivery -pipeline getting started
Flip the array gracefully
印象深刻的bug汇总(持续更新)
Dataset partitioning script for classification tasks
[Maui] add click events for label, image and other controls
kafuka学习之路(一)kafuka安装和简单使用
Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode
Wonderful! MarkBERT
redis中value/SortedSet
Xiaomi mobile phone unlocking BL tutorial
证券账户随便哪里开都能使用吗 开户安全吗
耐克如何常年霸榜第一名?最新財報答案來了
Shangtang entered the lifting period: the core management voluntarily banned and strengthened the company's long-term value confidence
Deep understanding of grpc part1
MySQL IN 和 NOT IN () 空列表报错
CPU 上下文切换的机制和类型 (CPU Context Switch)