+--+-------+-------------+ +-------+-------+--------+ | | | | | | | | | | Zone | | | | Zone | | | | | | -> | | | | | | show | | | | show | | | | | | | | | | +--+-------+-------------+ +-------+-------+--------+
我在C#中获得了一个基于this post的UserControl。当用户单击其中一个按钮时,我更改了Panel.Left属性,因此区域显示更改,使用左边缘计算它。我想在java中使用此功能。这是我在c#中执行的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TimeSheets.DataModels;
using System.Diagnostics;
namespace TimeSheets.UserControls
{
public partial class Tab : UserControl, TabItemClickListener
{
public TabListener Listener { get; set; }
private TabItem selectedItem;
private DateTime startTime;
public int ItemHeight { get; set; }
public int ItemWidth { get; set; }
public TabItem SelectedItem
{
get { return selectedItem; }
set
{
if (value != null)
{
selectedItem = value;
selectedIndex = panel.Controls.GetChildIndex(value, false);
CambiarSeleccion(selectedIndex);
}
}
}
private int selectedIndex;
public int SelectedIndex
{
get { return selectedIndex; }
set
{
selectedIndex = value;
if (selectedIndex >= 0 && selectedIndex <= panel.Controls.Count && panel.Controls.Count != 0)
CambiarSeleccion(selectedIndex);
}
}
public Tab()
{
InitializeComponent();
panel.Height = this.Height;
}
public void AddTabItem(TabItem item)
{
item.SetWidth(ItemWidth);
item.SetHeight(ItemHeight);
item.handler = this;
panel.Width += ItemWidth;
panel.Width += 6;
panel.Controls.Add(item);
}
public void LimpiarPaneles()
{
panel.Controls.Clear();
}
public TabItem GetTabItem(int position)
{
return (TabItem)panel.Controls[position];
}
public Object GetValueAt(int position)
{
if (selectedIndex >= 0 && selectedIndex <= panel.Controls.Count && panel.Controls.Count != 0)
return ((TabItem)panel.Controls[position]).Value;
else
return null;
}
private void tLeft_Tick(object sender, EventArgs e)
{
if (panel.Left < 0)
{
panel.Left = panel.Left + 5;
}
}
private void tRight_Tick(object sender, EventArgs e)
{
//Debug.WriteLine(panel.Width + "panel.Right" + panel.Right + "this.Left" + panel.Left + "panel1.Width" + panel1.Width);
if (panel.Width >= Math.Abs(panel.Left) + panel1.Width)
{
panel.Left = panel.Left - 5;
}
}
private void bRight_MouseUp(object sender, MouseEventArgs e)
{
tRight.Stop();
}
private void bRight_MouseDown(object sender, MouseEventArgs e)
{
startTime = DateTime.Now;
tRight.Start();
}
private void bLeft_MouseUp(object sender, MouseEventArgs e)
{
tLeft.Stop();
}
private void bLeft_MouseDown(object sender, MouseEventArgs e)
{
startTime = DateTime.Now;
tLeft.Start();
}
public new void Click(TabItem item)
{
selectedIndex = panel.Controls.GetChildIndex(item, false);
panel.Left = -selectedIndex * (ItemWidth + 6);
CambiarSeleccion(selectedIndex);
}
private void CallListener()
{
if (Listener != null)
Listener.OnItemSelected(selectedIndex);
}
private void bRight_Click(object sender, EventArgs e)
{
if ((DateTime.Now - startTime).Milliseconds <= 150)
{
if (selectedIndex < panel.Controls.Count - 1)
selectedIndex++;
CambiarSeleccion(selectedIndex);
}
}
private void bLeft_Click(object sender, EventArgs e)
{
if ((DateTime.Now - startTime).Milliseconds <= 150)
{
if (selectedIndex > 0)
selectedIndex--;
CambiarSeleccion(selectedIndex);
}
}
private void CambiarSeleccion(int newIndex)
{
if (selectedIndex >= 0 && selectedIndex <= panel.Controls.Count || panel.Controls.Count != 0)
{
if (selectedItem != null)
selectedItem.Deseleccionar();
CallListener();
selectedItem = GetTabItem(selectedIndex);
selectedItem.Seleccionar();
panel.Left = -selectedIndex * (ItemWidth + 6) - 2;
}
}
public void SetActivado(bool activo)
{
bLeft.Enabled = activo;
bRight.Enabled = activo;
}
}
public interface TabListener
{
void OnItemSelected(int selected);
}
}
现在我想把它翻译成Java。我完成了所有工作,但我找不到类似Left属性的东西来使用它。有什么我错过的吗?
答案 0 :(得分:1)
Swing使用布局管理器在其包含的组件中放置JPanel
。虽然discouraged,但您可以通过将其设置为null
来禁用组件中布局管理器的使用。如果这样做,您可以手动设置JPanels
的范围。
JPanel movingPanel, containingPanel;
/* initialize movingPanel and containingPanel
... */
containingPanel.setLayout(null);
movingPanel.setBounds(yourPanelDotLeft, up, width, heigth);
一些任意面板的示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SlidingControl {
Frame frame = new Frame("Sliding Control");
JPanel left = new JPanel(), right = new JPanel(), middle = new JPanel();
public static void main(String[] args) {
SlidingControl application = new SlidingControl();
application.movePanels();
}
public SlidingControl() {
frame.setSize(600, 200);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setResizable(false);
frame.setVisible(true);
initializePanels();
}
private void initializePanels(){
frame.setLayout(null);
frame.add(left);
frame.add(middle);
frame.add(right);
left.setBackground(new Color(1f, 0f, 0f));
middle.setBackground(new Color(0f, 1f, 0f));
right.setBackground(new Color(0f, 0f, 1f));
left.setBounds(10, 10, 50, 100);
middle.setBounds(400, 50, 50, 200);
right.setBounds(500, 50, 50, 200);
}
public void movePanels(){
while(true){
left.setBounds(left.getBounds().x - 5, left.getBounds().y, left.getBounds().width, left.getBounds().height);
middle.setBounds(middle.getBounds().x - 5, middle.getBounds().y, middle.getBounds().width, middle.getBounds().height);
right.setBounds(right.getBounds().x - 5, right.getBounds().y, right.getBounds().width, right.getBounds().height);
try{
Thread.sleep(600);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
答案 1 :(得分:1)
最后我开始工作,我的自定义Jpanel在西部和东部有两个按钮,中间是一个空布局面板,第一个面板内有一个流布局面板。当我按下鼠标时,它开始滑动,以显示DataTabItems(只是另一个带按钮的面板)。这个类的目的是从列表中选择一个项目并在任何地方显示。
这是DataTab的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.morethansimplycode.crud;
import com.morethansimplycode.data.Data;
import java.awt.Rectangle;
import java.util.List;
import javax.swing.Timer;
/**
*
* @author Oscar Date 21-may-2015 Time 22:15:09
*/
public class DataTab extends javax.swing.JPanel {
private final Timer tLeft = new Timer(6, (e) -> {
if (DataTab.this.tabContainer.getX() < 0)
left(5);
System.out.println("left" + DataTab.this.tabContainer.getX());
});
private final Timer tRight = new Timer(6, (e) -> {
if (DataTab.this.tabContainer.getWidth() >= Math.abs(DataTab.this.tabContainer.getX()) + DataTab.this.panel.getWidth())
left(-5);
System.out.println("right" + DataTab.this.tabContainer.getX());
});
private Data[] data;
/**
* Creates new form DataTab
*/
public DataTab() {
initComponents();
}
public void setData(Data[] data) {
this.data = data;
generateTabItems();
}
public void setData(List<Data> data) {
this.data = data.toArray(new Data[data.size()]);
generateTabItems();
}
private void generateTabItems() {
tabContainer.setBounds(tabContainer.getBounds().x, tabContainer.getBounds().y,tabContainer.getBounds().width + 10, panel.getBounds().height);
for (Data d : data) {
DataTabItem item = new DataTabItem(d, 275, 275);
tabContainer.add(item);
tabContainer.setSize(tabContainer.getWidth() + item.getPreferredSize().width + 10, tabContainer.getHeight());
System.out.println(tabContainer.getComponentCount() + " " + item.getSize());
}
this.updateUI();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bLeft = new javax.swing.JButton();
bRight = new javax.swing.JButton();
panel = new javax.swing.JPanel();
tabContainer = new javax.swing.JPanel();
setMinimumSize(new java.awt.Dimension(85, 40));
setPreferredSize(new java.awt.Dimension(200, 20));
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
resize(evt);
}
});
setLayout(new java.awt.BorderLayout());
bLeft.setText("<");
bLeft.setMargin(new java.awt.Insets(0, 0, 0, 0));
bLeft.setMaximumSize(new java.awt.Dimension(10, 19));
bLeft.setMinimumSize(new java.awt.Dimension(10, 19));
bLeft.setPreferredSize(new java.awt.Dimension(40, 10));
bLeft.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
bLeftPressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
bLeftReleased(evt);
}
});
bLeft.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bLeftActionPerformed(evt);
}
});
add(bLeft, java.awt.BorderLayout.WEST);
bRight.setText(">");
bRight.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
bRightMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
bRightMouseReleased(evt);
}
});
bRight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bRightActionPerformed(evt);
}
});
add(bRight, java.awt.BorderLayout.EAST);
panel.setBackground(new java.awt.Color(255, 255, 255));
panel.setLayout(null);
tabContainer.setBackground(new java.awt.Color(102, 0, 204));
tabContainer.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 5));
panel.add(tabContainer);
tabContainer.setBounds(0, 0, 0, 40);
add(panel, java.awt.BorderLayout.CENTER);
}// </editor-fold>
private void bRightActionPerformed(java.awt.event.ActionEvent evt) {
}
private void bLeftActionPerformed(java.awt.event.ActionEvent evt) {
}
private void resize(java.awt.event.ComponentEvent evt) {
System.out.println(tabContainer.getBounds());
tabContainer.setBounds(tabContainer.getBounds().x, tabContainer.getBounds().y,tabContainer.getBounds().width, panel.getBounds().height);
System.out.println(tabContainer.getBounds());
this.updateUI();
}
private void bLeftPressed(java.awt.event.MouseEvent evt) {
tLeft.start();
}
private void bLeftReleased(java.awt.event.MouseEvent evt) {
tLeft.stop();
}
private void bRightMousePressed(java.awt.event.MouseEvent evt) {
tRight.start();
}
private void bRightMouseReleased(java.awt.event.MouseEvent evt) {
tRight.stop();
}
private void left(int movePixels) {
Rectangle r = tabContainer.getBounds();
r.x += movePixels;
tabContainer.setBounds(r);
}
// Variables declaration - do not modify
private javax.swing.JButton bLeft;
private javax.swing.JButton bRight;
private javax.swing.JPanel panel;
private javax.swing.JPanel tabContainer;
// End of variables declaration
}
这是DataTabItem类:
package com.morethansimplycode.crud;
import com.morethansimplycode.data.Data;
import java.awt.Dimension;
/**
*
* @author Oscar
* Date 21-may-2015
* Time 22:26:21
*/
public class DataTabItem extends javax.swing.JPanel {
Data value;
/** Creates new form DataTabItem */
public DataTabItem() {
initComponents();
}
public DataTabItem(Data d, int height, int width) {
initComponents();
value = d;
button.setPreferredSize(new Dimension(width, height));
this.setPreferredSize(new Dimension(width, height));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
button = new javax.swing.JButton();
setBackground(new java.awt.Color(255, 255, 255));
setLayout(new java.awt.BorderLayout());
button.setBackground(new java.awt.Color(102, 102, 255));
button.setText("PACO");
button.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
add(button, java.awt.BorderLayout.CENTER);
}// </editor-fold>
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("hola");
}
// Variables declaration - do not modify
private javax.swing.JButton button;
// End of variables declaration
}
感谢其他答案,并希望此代码可以帮助任何想要这个的人。